
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 −
Example
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]))
Input
[2, 3, 4, 9, 7, 13]
Output
2