Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - how do I multiply integers in a set?

You are given a sequence of positive ints where every element appears three times, except one that appears only once (let's call it x) and one that appears only twice (let's call it y). Your task is to find x * x * y.

e.g. arr = [1,1,1,2,2,2,3,3,4] -> 4 x 4 x 3

I have written some code below. I have a question regarding the final part of the code- so after the completion of the loop, there should be one integer left in seen_once and one integer left in seen_twice, but how do I then multiply these numbers, as they are now sitting in a set()?

 def Missing_Values(arr):
     seen_once = set()
     seen_twice = set()
     seen_thrice = set()

     for i in arr:
         if i not in seen_once or seen_twice or seen_thrice:
             seen_once.add(i)

         elif i in seen_once:
             seen_twice.add(i)
             seen_once.remove(i)

         elif i in seen_twice:
             seen_thrice.add(i)
             seen_twice.remove(i)

     return seen_once*seen_once*seen_twice

 Missing_Values(arr)
question from:https://stackoverflow.com/questions/66063813/how-do-i-multiply-integers-in-a-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way would be to pop the values.

x = seen_once.pop()
y = seen_twice.pop()
return x * x * y

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...