- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bob's Game in Python
Suppose we have a friend named Bob, and he is playing a game with himself. He gives himself a list of numbers called nums. Now in each turn, Bob selects two elements of the list and replaces them with one positive integer with the same sum as the numbers he selected. Bob declares the victory when all of the number in the array are even. We have to find the minimum number of turns are required to make by Bob, so he can declare victory, if there is no such solution, then return -1.
So, if the input is like [2, 3, 4, 9, 7, 13], then the output will be 2 as he can take 3,9 then replace with 12, then take 7,13 and replace with 20.
To solve this, we will follow these steps −
a := a list by taking only odd elements from numes
if size of a is odd, then
return (size of a)/2
otherwise return -1
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums): a = [x for x in nums if x %2 == 1] if len(a) %2 == 0: return len(a)/2; return -1; ob = Solution() print(ob.solve([2, 3, 4, 9, 7, 13]))
[2, 3, 4, 9, 7, 13]
- Conway’s Game Of Life using Python?
- Hangman Game in Python?
- Jump Game in Python
- Baseball Game in Python
- Hangman Game in Python Programming
- Jump Game II in Python
- Color game using Tkinter in Python
- Binary Tree Coloring Game in Python
- How to develop a game in Python?
- Catching the ball game using Python
- Program to fill Min-max game tree in Python
- Program to find winner of stone game in Python
- Stone Game in C++
- Elimination Game in C++
- Dungeon Game in C++