I have written the following block:
number = 12
def checkio(number):
count = 0
for n in bin(number):
print n
if n == 1:
count += 1
print count
checkio(number)
The output I get is:
0
0
b
0
1
0
1
0
0
0
0
0
I can't understand why I'm able to iterate through n in the binary number and print it, but my if won't work correctly and won't add to my count variable.
Why does this happen?
When you iterate through the string produced by bin, each character is itself a one-character string. So the reason this doesn't work is, simply:
1 != '1'
You will need to convert the characters back into integers to compare (be aware that int('b') won't work!), or compare to a string:
if n == '1':
The check should be "if n == '1'".
Related
I need to create a program in which it allows the user to choose the quantity of perfect combinations that they want to see. A combination is perfect when the number is multiple of 3 and 5. For example if I have input 5 the program will need to start from 0 and once it reaches the number that is perfect shows a "perfect" each time it is perfect
I have been trying to use:
combi = (int(input())
count = 0
for i in range(combi):
if (i % 3 == 0 and i % 5 == 0):
print("Perfect")
I do not know how to make it count since 0 until we have 5 numbers which are perfect
Use the break statement:
combi = (int(input())
count = 0
for i in range(combi):
if (i % 3 == 0 and i % 5 == 0):
print("Perfect")
count += 1
if count == 5:
break
I was looking into a method for finding the number of trailing zeros in binary numbers and came across a solution in C (link). I am looking for a solution in Python!
Binary Input -> 1000
Output: 3
Binary Input -> 101101001100
Output: 2
Binary Input -> 1010001010000
Output: 4
Binary Input -> 100000001
Output: 0
Is there an efficient way of doing this without iterating the binary number as a string or using string methods for filtering? Basically, I may have a very large number of very very large binary numbers, so I am trying to find something more efficient than simply iterating over it as a string.
EDIT:
Here is my attempt -
def trailingzeros(l):
count = 0
a = [i for i in str(l)]
for i in reversed(a):
if i=='0':
count+=1
else:
break
return count
NOTE: I am looking for a solution that exploits the binary nature of the input.
n = 0b1010001010000
count = 0
while n:
if (n&1):
break
n >>= 1
count += 1
print(count)
Prints:
4
You can use python bit operators:
def findTrailing0(num):
count = 0
lastBit = 0
while num != 0:
lastBit = num & 0x1
if lastBit != 0:
break
count += 1
num >>= 1
return count
I'm trying to create a function that take two parameters: D = digit (0-9) and n = positive number.
If the D is parity number, the function should give me 0 but ,if the D is odd number, the function should count numbers of odd number I have in n.
There is a problem with this code but I don't know what:
def testD(D,n):
if D % 2 == 0:
return 0
count = 0
while n > 0:
if(n%10) %2==1:
count +=1
n=n/10
return count
I changed 2 things :
while n > 1: instead of while n > 0: otherwise your loop never stops
n=n//10 instead of n=n/10, where // is the euclidian division, which is what you need here
You should try this :
def testD(D,n):
if D % 2 == 0:
return 0
count = 0
while n > 1:
if(n%10) %2==1:
count +=1
n=n//10
return count
print(testD(7, 555))
# output : 3 (because 7 is odd, and there is 3 odd digits in 555)
I'm having trouble with this homework problem.
SPECIAL_SYMBOLS = '!##$%^&*()_+=[]?/'
def symbol_count(s:str): -> int
"""Return the largest number of consecutive "special symbols" in the
string s.
>>> symbol_count(’c0mput3r’)
0
>>> symbol_count(’H! [here’)
1
>>> symbol_count(’h3!!&o world##’)
3
"""
lst = []
count = 0
for i in range(len(s)-1):
if s[i] in SPECIAL_SYMBOLS:
count +=1
if s[i+1] not in SPECIAL_SYMBOLS:
lst.append(count)
count = 0
else:
count += 1
if lst == []:
return 0
return max(lst)
however, for the last example i get 5 instead of 3 so im guessing my count is not reinitializing to 0. I was wondering why this was the case. Thank you for the help.
You problem is here:
else:
count += 1
It ends up double counting consecutive special characters. Just remove both of those lines.
I'm really a beginner with python and in classe I'm analyzing a coin toss task. The number of tosses is 1000, the results possible are 1,2. I'm asked to create row with sequences of same results (such as 1 1 1 1 1 1 1 1 and then 2 2 2 2 2,..) and give the length of the longest appearing sequence.
from numpy.random import randint, seed
seed(0)
for n in range(1000):
r = randint(1,3)
print(r)
which gives me a single column reporting the results as follows
1
2
1
1
2
1
1
I can't manage to find the appropriate code to create those rows of sequences of same results.
You are printing the result of each iteration of your for loop.
A simple solution is to create 2 lists, each containing every occurrence of either 1 or 2:
list1 = []
list2 = []
In your for loop, you can use the list.append method to add the value of r to the corresponding list:
for n in range(1000):
r = randint(1, 3)
if r == 1:
list1.append(r)
else:
list2.append(r)
This way, list1 contains every occurrence of the number 1, and list2 contains all the number 2.
You can now print both list, and use len() to get the number of elements in each list.
Try this:
count1 = 0
count2 = 0
for n in range(1000):
r = randint(1, 3)
if r == 1:
count1 += 1
elif r == 2:
count2 += 1
if count1 > count2:
print('1'*count1)
print('2'*count2)
print('Longest sequence is of 1\'s, with %s occurences' %count1)
else:
print('1'*count1)
print('2'*count2)
print('Longest sequence is of 2\'s, with %s occurences' %count2)