Nuts and Bolts Problem Python
PROGRAM TO SOLVE NUT AND BOLTS PROBLEM (LOCK AND KEY PROBLEM)
OUTPUT
Matched nuts and bolts are : # $ % & @ ^ # $ % & @ ^
# Function to match nuts and
# bolts
def
nutboltmatch(nuts,
bolts, n):
hash1
=
{}
# creating a hashmap
# for nuts
for
i
in
range
(n):
hash1[nuts[i]]
=
i
# searching for nuts for
# each bolt in hash map
for
i
in
range
(n):
if
(bolts[i]
in
hash1):
nuts[i]
=
bolts[i]
# Print the result
print
(
"matched nuts and bolts are-"
)
for
i
in
range
(n):
print
(nuts[i],
end
=
" "
)
print
()
for
i
in
range
(n):
print
(bolts[i],
end
=
" "
)
# Driver code
if
__name__
=
=
"__main__"
:
nuts
=
[
'@'
,
'#'
,
'$'
,
'%'
,
'^'
,
'&'
]
bolts
=
[
'$'
,
'%'
,
'&'
,
'^'
,
'@'
,
'#'
]
n
=
len
(nuts)
nutboltmatch(nuts, bolts, n)
Matched nuts and bolts are : # $ % & @ ^ # $ % & @ ^
Comments
Post a Comment