Want to print consecutive 1's in - python-3.x

from array import *
n=int(input("Enter the number"))
m=bin(n)
#print(m)
a=array("u",[])
a=int(m[2:])
print(a)
count=0
for i in range(a):
if(a[i]=="1" and a[i+1]=="1"):
count=count+1
print(count)
Here i want to print'ONLY' the number of consecutive 1's that appear in a binary number.

If your logic is correct try this,
n = int(input('Enter a number : '))
nb =bin(n)
l = list(nb)[2:]
count = 0
for i in range(len(l)-1):
if l[i]=='1' and l[i+1]=='1':
count += 1
print(count)

Related

Display sum of every number from any specific range with User input

Using below code , i want to display sum for a particular 'n' range lies between two numbers.
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
i = 1
for i in range(start, end+1):
i+=1
print(i)
Yes, it concatenates two numbers by +1, now to display each number's sum one by one or if i want sum of all numbers from 1 to 100 (Say,1+2+3+4+5+6+7...n) or any range 'n' entered in particular, then how to go. Thanks
One of the Pythonic ways is
from itertools import accumulate
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
for index, value in enumerate(accumulate(range(start, end+1))):
print(f'Sum from {start} to {index} is : {value}')
Output is the same as from #SuyogShimpi, not really pythonic, more C-style, but maybe easier to understand.
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
current_sum = 0
for i in range(start, end+1):
current_sum += i
print(f'sum from {start} to {i} is {current_sum}')
you can use Numpy
Installing
pip install numpy
and use numpy.sum
import numpy as np
start = int(input("Enter the number : "))
end = int(input("Enter the number : "))
Sum = np.sum(np.arange(start, end+1))
print(Sum)

How to count consecutive characters?

Need to find the count of each consecutive character in a row.
Ex: aaaabbBBccaa
output: a4b2B2c2a2
The character may repeat but need to count only consecutive ones. I also need to maintain the original sequence.
I tried to do this by code below but it doesn't work.
l = input()
counter = dict()
if l.isalpha() == True:
for letter in l:
if letter in counter:
counter[letter] += 1
else:
counter[letter] = 1
for this_one in list(counter.keys()):
print(this_one,counter[this_one],sep ="",end="")
Solution if you are interested in the while loop mechanics :
l = 'aaaabbBBccaazzZZZzzzertTTyyzaaaAA'
output = ''
index = 0
while index < len(l):
incr = index
count = 1
output += l[incr]
while incr < len(l)-1 and l[incr]==l[incr+1]:
count += 1
incr += 1
index += 1
output += str(count)
index += 1
print(output)
itertools.groupby allows you to express this quite concisely using a generator expression.
from itertools import groupby
''.join(f'{x}{len(list(y))}' for x, y in groupby('aaaabbBBccaa'))
# outputs:
'a4b2B2c2a2'

I was supposed to count the unique digits in a number but getting this int object not iterable on 4th line and not sure how to fix it

x = int(input("Enter the number: "))
count = 0
for elements in range(0,10):
for i in (x):
if elements == i:
count += 1
break
print()
That error raise because an integer is not an iterate object unlike strings, list, etc. So what you can do it's just work with a string, then use set (which get the unique values) and then get the length of it and you won't need to traverse with a for loop as below:
x = input("Enter the number: ")
unique_digits = set(x)
print(len(unique_digits))
Hope it will help you :)
x = input("Enter number: ")
count = 0
for elements in range(10):
for t in x:
if (int(t)==elements):
count += 1
break
print(count)

Separating positive even, positive odd, negative odd, and negative even numbers into new arrays

I am trying to separate the -odd, -even, even, and odds into separate arrays. I have done this in matlab but confused with how this would work in python. All I got so far is how to generate a user inputted array
print('Enter 10 numbers: ')
num=10
l1=[0]*num
for l in range (0,num):
numbers = float(input('Enter value #'+str(l+1)+' : '))
l1[l]=numbers
print('Your numbers are: ',l1 )
Here's a working example that does what you need and starts with your code to populate the "l1" list.
negative_odds = []
negative_evens = []
evens = []
odds = []
for num in l1:
if num % 2 == 0:
if num < 0:
negative_evens.append(num)
else:
evens.append(num)
else:
if num < 0:
negative_odds.append(num)
else:
odds.append(num)
print('-odd: ', negative_odds)
print('-even: ', negative_evens)
print('even: ', evens)
print('odd: ', odds)

What is wrong with this code that tries to print a decrementing sequence of numbers?

I am very new to python and want to write a program that counts down. It has to start at 100 and ends at 1 or 0. How do i have to do that?
This is what i've got now:
def countdown(n):
while n > 0:
print (n)
n = n =2**123
print('Blastoff')
countdown(200)
n = n =2**123
??? What is this supposed to do? What are you trying to accomplish by setting n to 2 to the 123th power? I think
n = n - 1
or
n -= 1
would be more appropriate.
Here is the code:
#!/usr/bin/python
def countdown(count):
while (count >= 0):
print ('The count is: ', count)
count -= 1
countdown(10)
print ("Good bye!")
If you want to it count down in the terms of actual seconds, which I'm going to guess is what you are going for it would be done by causing the countdown to sleep for 1 second at each iteration:
#!/usr/bin/python
import time
def countdown(count):
while (count >= 0):
print ('The count is: ', count)
count -= 1
time.sleep(1)
countdown(10)
print ("Good bye!")
The output is:
The count is: 10
The count is: 9
The count is: 8
The count is: 7
The count is: 6
The count is: 5
The count is: 4
The count is: 3
The count is: 2
The count is: 1
The count is: 0
Good bye!
Let me know if you have any questions.
In python, the indentation matters. All of the lines inside the function have to be indented.
Your method should have been:
def countdown(n):
while n > 0:
print (n)
n = n-1
print("Blastoff")
or a more pythonic way could be:
def countdown(n):
for i in reversed( range(n) ):
print i
print "Blastoff"
Easy way is using range with negative increment parameter.
For example:
for n in range(10,0,-1):
print(n)
Another way: you can use yield command. It's using for making a generator. It's like to return command.
For example:
#this is generator function
def countdown(start,last):
n=start
while(n>last):
yield n
n-=1
for n in countdown(10,0):
print(n)
i would simply wright something like:
import time
num1 = 100
num2 = 0
while (num1 > num2):
print num1
num1 = num1 - 1
time.sleep(1)
Hay Try This You Can Input The Number You Need To Count Down From:
import time
numTimes = int(input("How Many Seconds Do You Wish To Have Untill LiftOff: "))
def countdown(count):
while (count >= 0):
print ("LiftOff In: ", count)
count -= 1
time.sleep(1)
countdown(numTimes)
print ("!WE HAVE LIFT OFF!")
The following code should work:
import time
start = int(input("How many seconds do you want?\nSeconds: "))
for i in range(start,-1,-1):
time.sleep(1)
print(i)
print "Countdown finish!"

Resources