For a homework problem, we are asked to define a function that will count the number of consecutive digits in a binary string, and return the number.
For example, the function should return n = [4,8,4,3,15] for the binary input S = ‘1111000000001111000111111111111111’.
I have this so far, but I know it's not correct, and I do not know where to go from here. Any help would be appreciated!
def consecutive_length(s):
if s == '':
return 0
if s[0] == 0:
return 0
return 1 + consecutive_length(s[1:])
Note: we cannot use any loops. It is required that we do this with recursion.
Thank you!
Here's a hopefully pythonic way (ignoring the fact that it's not pythonic to solve this kind of problem recursively):
def consecutive_length(s):
def sub(idx, lst, last_char, count):
try:
c = s[idx] # c will be the 'next' char
except IndexError: # no more chars left to process
if count:
lst.append(count)
return lst
if c != last_char:
lst.append(count)
count = 0
return sub(idx+1, lst, c, count+1)
return sub(0, [], s[0] if s else None, 0)
where
the outer function just takes the string as an argument and hides the inner functions additional parameters
idx is the index to the string, we don't allocate a new string at every recursive call (and s[idx] is O(1) iirc)
instead of computing the length of the string, we wait for an exception to happen (EAFP - Easier to ask for forgiveness than permission)
Testing:
>>> print consecutive_length('1111000000001111000111111111111111')
[4, 8, 4, 3, 15]
>>> print consecutive_length('1111000000001111000111111111111110')
[4, 8, 4, 3, 14, 1]
>>> print consecutive_length('1')
[1]
>>> print consecutive_length('0')
[1]
>>> print consecutive_length('')
[]
I am assuming here the '11' is a consecutive sequence of 1's .So '111' has 2 consecutive one's. This solution is, if loops is not a problem. Use index to find '11' and keep doing it until you find no more. The below program shows the number of consecutive 1's .
cnt = 0
pos = -1
while True:
try:
pos = '111001100101111'.index('11', pos+1)
cnt += 1
except ValueError:
print cnt
break
Result:
6
EDIT: uselpa has a much better way of doing it.
Since loops aren't allowed:
def consecutive_length(s, output, prev_char, count):
# if end of string, append last count and return output
if s == '':
output.append(count)
return output
# if curr_char is same as prev_char, add 1 to count and parse next char
if s[0] == prev_char:
return consecutive_length(s[1:], output, s[0], count + 1)
# if curr_char is diff from prev_char, append count and reset count to 1
else:
prev_char = s[0]
output.append(count)
return consecutive_length(s[1:], output, s[0], 1)
Call it with consecutive_length(s, [], s[0], 0).
Related
I need to write code that returns 2 index numbers of an array. The function takes 2 arguments, one is an array and the other is an integer.
I need to check if two of the values inside the array adds up to the integer and using the numbers inside the array only once.
Here is my code:
def func(a,b):
for i in a:
cnt = 0
while cnt < len(a):
if i + a[cnt] == b and i != a[cnt]:
return list([i,a[cnt]])
else:
cnt += 1
print(func([3,7,2,10,20],27))
My output for func([3, 7, 2, 10, 20], 27) is [7, 20].
This code shows that the loop can find the numbers which add up to the integer.
But when I do this:
def func(a,b):
for i in a:
cnt = 0
while cnt < len(a):
if i + a[cnt] == b and i != a[cnt]:
return a.index(i,a[cnt])
else:
cnt += 1
print(func([3,7,2,10,20],27))
I get the Value error: 7 not in list, which clearly is.
I've had this issue working with other exercises as well.
Am I doing something wrong or the index function isn't suppose to be used like that.
What would be an efficient way to return the index numbers without having to write another loop for it?
The second parameter to index that you're passing is actually the starting index on the list in which the search for the element will start (as you can see here). If you remove it, you'll see that it returns the first value you want (but not the second). It is relevant to note that the index method will only ever return the first occurrence of the value.
def func(a,b):
for i in a:
cnt = 0
while cnt < len(a):
if i + a[cnt] == b and i != a[cnt]:
return a.index(i)
else:
cnt += 1
print(func([3,7,2,10,20],27))
>>> 1
This happens because the value you're passing as the starting index (a[cnt]) is greater than the actual index of the number (1).
By removing it, you search through all the list, and find the correct value (remembering that Python uses zero-indexed iterators).
But since you want to return a list with both values, you need to explicitly state you want the index for each, such as:
def func(a,b):
for i in a:
cnt = 0
while cnt < len(a):
if i + a[cnt] == b and i != a[cnt]:
return [a.index(i), a.index(a[cnt])]
else:
cnt += 1
print(func([3,7,2,10,20],27))
>>> [1, 4]
You could achieve the same results using two for loops, however, in a cleaner way (also gave meaningful names to variables):
def find_indices_for_sum(array, sum_value):
for i in array:
for j in array:
if i + j == sum_value and i != j:
return [array.index(i), array.index(j)]
print(find_indices_for_sum([3,7,2,10,20],27))
>>> [1, 4]
If you want to be able to deal with equal numbers, you can change the comparison strategy altogether, using indices instead of values, since the former are unique in a list, but the latter are not. enumerate is a good option here, since it allows to iterate both through index and values at the same time in a clean way.
def find_indices_for_sum(array, sum_value):
for i, value_i in enumerate(array):
for j, value_j in enumerate(array):
if i != j and value_i + value_j == sum_value:
return [i, j]
print(find_indices_for_sum([3,3],6))
>>> [0, 1]
So I am new to recursion and I am trying to make a program where you can enter a list and python tests each integer (lets say 9 for example) and sees if the integer following it is doubled. So if I entered a list of 2 4 8 16 32, would return 4, and -5 -10 0 6 12 9 36, would return 2 because -5 followed by -10 is one and 6 followed by 12 is the second. This is the code I have so far. I feel like I am very close. but just a few thing stand in my way. Any help would be great!
L = []
def countDouble(L):
x = input(f'Enter a list of numbers separated by a space: ')
y = (x.split(' '))
print(y[1])
print(y[0])
count = 0
y[0] += y[0]
# unsure of how to multiple y[0] by 2
if y[0]*2 == y[1]:
count += 1
else:
count += 0
#how would I traverse through the rest of the entered list using recursion?
print(count)
countDouble(L)
If you want/need to solve it using recursion, the following will do the trick:
def count_sequential_doubles(li, count=0):
return count_sequential_doubles(li[1:], count + int(li[0] * 2 == li[1])) if len(li) > 1 else count
I would suggest this recursive way:
def countDouble(L):
count = 0
if len(L) == 1:
return count
else:
if int(L[0])*2 == int(L[1]):
count += 1
return count + countDouble(L[1:])
x = input(f'Enter a list of numbers separated by a space: ')
y = (x.split(' '))
count = countDouble(y)
print(count)
I urge you to read the entire answer, but in case you are not interested in tips, notes and the process of finding the solution, here are two solutions:
solution using recursion (not recommended):
x = input()
y = x.split(' ')
count = 0
def countDouble(i):
if(i+1 == len(y)):
return 'recursion ends here when'
if(int(y[i])*2==int(y[i+1])):
count += 1
countDouble(i+1)
countDouble(0)
print(count)
this solution just imitates a while loop:
solution using a while loop (recommended):
x = input()
y = x.split(' ')
count = 0
i = 0
while(i < len(y) - 1):
if(int(y[i]) * 2 == int(y[i+1])):
count += 1
i += 1
print(count)
Before I continue, here are a few tips and notes: (some of them will only make sense after)
I assume the 14 in your example is a typo
I didn't put the code in a function because it's not needed, but you can change it easily.
In your code, you are passing L as a parameter to the countDouble() function, but you don't use it. if you don't need a parameter don't pass it.
when splitting the input, the values of the list are still strings. so you have to invert them to integers (for instance, you can do that with the int() 'function') before comparing their values - otherwise multiplying by 2 will just repeat the string. for example: '13'*2 is the string '1313'
I don't know why you why you added y[0] to itself in line 9, but based on the code that comes after this would yield incorrect results, you don't need to change the elements in order to get their value multiplied by 2.
notice that in the else block, nothing has changed. adding 0 to the count doesn't change it. so you can remove the else block entirely
While it's possible to solve the problem in recursion, there's something else designed for these kind of problems: loops.
The problem is essentially repeating a simple check for every element of a list.
This is how I would arrive to a solution
so we want to run the following 'code':
if(y[0]*2 == y[1]):
count += 1
if(y[1]*2 == y[2]):
count += 1
if(y[2]*2 == y[3]):
count += 1
...
of course the computer doesn't understand what "..." means, but it gives us an idea to the pattern in the code. now we can do the following:
divide the extended 'code' into similar sections.
identify the variables in the pattern - the values that change between sections
find the starting values of all variables
find a pattern in the changes of each variable
find a breaking point, a condition on one of the variables that tells us we have reached the last repeating section.
here are the steps in this specific problem:
the sections are the if statements
the variables are the indexes of the elements in y we compare
the first index starts at 0 and the second at 1
both indexes increase by one after each if-statement
when the second index is bigger then the last index of y then we already checked all the elements and we can stop
so all is left is to set the needed variables, have a while loop with the breaking condition we found, and in the while loop have the general case of the repeating sections and then the changing of the variables.
so:
x = input(f'Enter a list of numbers separated by a space: ')
y = (x.split(' '))
count = 0
# setting the starting values of the variables
index1 = 0
index2 = 1
# creating a loop with the breaking condition
while(index2 < len(y)):
# the general case of the repeated code:
if(int(y[index1]) * 2 == int(y[index2])):
count += 1
# changing the variables for the next loop
index1 += 1
index2 += 1
print(count)
We see that the index2 is just index1 + 1 at all time. so we can replace it like that:
x = input(f'Enter a list of numbers separated by a space: ')
y = (x.split(' '))
count = 0
index1 = 0
while(index1 + 1 < len(y)):
if(int(y[index1]) * 2 == int(y[index1 + 1])):
count += 1
index1 += 1
print(count)
Note: You can use a for loop similarly to the while loop
So in summary, you can use recursion to solve the problem, but the recursion would just be imitating the process of a loop:
in each call, the breaking condition will be checked, the repeated code would run and the variables/parameters would change.
Hope you find this answer useful :)
Final edit: OP edited his example so my other code didnt apply
Some good questions people are asking, but in the spirit of helping, here's a recursive function that returns the count of all doubles.
def get_doubles_count_with_recursion(a_list, count, previous=None):
while a_list:
try:
first = previous if previous else a_list.pop(0)
next_item = a_list.pop(0)
except IndexError:
return count
if next_item / 2 == first:
count += 1
return get_doubles_count_with_recursion(a_list, count, next_item)
return count
a_list = [1, 3, 5, 10, 11, 14, 28, 56, 88, 116, 232, 464, 500]
doubles = get_doubles_count_with_recursion(a_list, 0)
print(doubles == 5)
Probably could clean it up a bit, but it's a lot easier to read than the other guy's ;)
If I'm reading your question right, you want a count of all pairs where the 2nd item is double the first. (and the 14 in the first list is a typo). In which case a simple function like this should do the job:
#a = [2,4,8,16,32]
a = [-5, -10, 0, 16, 32]
count = 0
for i, x in enumerate(a):
# Stop before the list overflows
if i < len(a) - 1:
# If the next element is double the current one, increment the counter
if a[i+1] == x * 2:
count = count + 1
else:
break
print(count)
I am trying to make a string alternate between upper and lower case letters. My current code is this:
def skyline (str1):
result = ''
index = 0
for i in str1:
result += str1[index].upper() + str1[index + 1].lower()
index += 2
return result
When I run the above code I get an error saying String index out of range. How can I fix this?
One way using below with join + enumerate:
s = 'asdfghjkl'
''.join(v.upper() if i%2==0 else v.lower() for i, v in enumerate(s))
#'AsDfGhJkL'
This is the way I would rewrite your logic:
from itertools import islice, zip_longest
def skyline(str1):
result = ''
index = 0
for i, j in zip_longest(str1[::2], islice(str1, 1, None, 2), fillvalue=''):
result += i.upper() + j.lower()
return result
res = skyline('hello')
'HeLlO'
Explanation
Use itertools.zip_longest to iterate chunks of your string.
Use itertools.islice to extract every second character without building a separate string.
Now just iterate through your zipped iterable and append as before.
Try for i in range(len(str1)): and substitute index for i in the code. After, you could do
if i % 2 == 0: result += str1[i].upper()
else: result += str1[i].lower()
For every character in your input string, you are incrementing the index by 2. That's why you are going out of bounds.
Try using length of string for that purpose.
you do not check if your index is still in the size of your string.
It would be necessary to add a condition which verifies if the value of i is always smaller than the string and that i% 2 == 0 and that i == 0 to put the 1st character in Upper
with i% 2 == 0 we will apply the upper one letter on two
for i, __ in enumerate(str1):
if i+1 < len(str1) and i % 2 == 0 or i == 0:
result += str1[i].upper() + str1[i + 1].lower()
I tried to modify as minimal as possible in your code, so that you could understand properly. I just added a for loop with step 2 so that you wouldn't end up with index out of range. And for the final character in case of odd length string, I handled separately.
def skyline (str1):
result = ''
length = len(str1)
for index in range(0, length - 1, 2):
result += str1[index].upper() + str1[index + 1].lower()
if length % 2 == 1:
result += str1[length - 1].upper()
return result
You can use the following code:
def myfunc(str1):
result=''
for i in range(0,len(str1)):
if i % 2 == 0:
result += str1[i].upper()
else:
result += str1[i].lower()
return result
in your code you are get 2 word by one time so you should divide your loop by 2 because your loop work by depending your input string so make an variable like peak and equal it to len(your input input) then peak = int(peak/2) it will solve your pr
def func(name):
counter1 = 0
counter2 = 1
string = ''
peak = len(name)
peak = int(peak/2)
for letter in range(1,peak+1):
string += name[counter1].lower() + name[counter2].upper()
counter1 +=2
counter2 +=2
return string
I am trying to write a function. This function takes a list of numbers as input, finds the largest sequence of consecutive numbers in this list and returns a list containing only this largest sequence of numbers of the original list.
Example:
In [2]: largestSeq([1,2,3,4,1,2,3])
Out[2]: [1, 2, 3, 4]
It works as long as the input list has 0 or more than 1 elements in it.
I included print statements in my code to see where the error is.
Here is the code and the result of calling largestSeq([1])and largestSeq([1,2]):
Code:
def findSeq(seq): #this finds a sequence of consecutive numbers
i = 0 #in a list and returns it
if len(seq) <= 1: #it stops when the next number in the list
return seq #is smaller than the former
s =[seq[0]]
while seq[i] < seq[i+1]:
i += 1
s.append(seq[i])
if i == len(seq)-1:
break
return s
def largestSeq(seq,a=[]):
b = findSeq(seq) #find the first consecutive sequence
if len(seq) == 0:
return a
print 'Length of b is ' + str(len(b))
if len(b) > len(a): #check if found sequence is bigger than
print 'seq is now ' + str(seq)#the last found sequence
print 'b is now ' + str(b)
i = len(b)
print 'now deleting elements of seq'
for d in range (i):
seq.remove(seq[0]) #remove found sequence from the original
#del seq[0:i] #list
print 'seq is now ' + str(seq)
print 'b is now ' + str(b)
return largestSeq(seq,b) #start over
else:
del seq[0:len(b)]
return largestSeq(seq,a)
And now the calls:
In [14]: largestSeq([1])
Length of b is 1
seq is now [1]
b is now [1]
now deleting elements of seq
seq is now []
b is now []
Out[14]: []
largestSeq([1,2])
Length of b is 2
seq is now [1, 2]
b is now [1, 2]
now deleting elements of seq
seq is now []
b is now [1, 2]
Out[15]: [1, 2]
Please note, that in the first call, the element in b is also deleted after deleting element of seq , althoug I didn't change it!
In the second call with [1,2] b is behaving like I want it, while seq is deleted.
I tried manipulating the list with list.remove and with del (which is commented out and yields the same error).
What is going on in there? I don't understand it.
I want b remaining unchanged in the first call, like it does in the second call.
This is a very specific question. I would be grateful for any suggestions!
In the first case you are returning the same list, you have to return a copy of the list.
Try:
def findSeq(seq): #this finds a sequence of consecutive numbers
i = 0 #in a list and returns it
if len(seq) <= 1: #it stops when the next number in the list
return list(seq) #is smaller than the former
s =[seq[0]]
while seq[i] < seq[i+1]:
i += 1
s.append(seq[i])
if i == len(seq)-1:
break
return s
I'm new to programming and I'm trying to do the codingbat.com problems to start. I came across this problem:
Given an array calculate the sum except when there is a 13 in the array. If there is a 13 in the array, skip the 13 and the number immediately following it. For example [1,2,13,5,1] should yield 4 (since the 13 and the 5s are skipped).
This is what I have so far. My problem is that I don't know what to do when there are multiple 13s...And I would like to learn coding efficiently. Can you guys help? (I'm using python 3.2) Thanks!
def pos(nums):
for i in nums:
if i == 13:
return nums.index(13)
return False
def sum13(lis):
if pos(lis)!= False:
return sum(lis[:pos(lis)])+sum(lis[pos(lis)+1:])
else:
return sum(lis)
One tricky thing to notice is something like this: [1, 13, 13, 2, 3]
You need to skip 2 too
def getSum(l):
sum = 0
skip = False
for i in l:
if i == 13:
skip = True
continue
if skip:
skip = False
continue
sum += i
return sum
Explanation:
You go through the items in the list one by one
Each time you
First check if it's 13, if it is, then you mark skip as True, so that you can also skip next item.
Second, you check if skip is True, if it is, which means it's a item right after 13, so you need to skip this one too, and you also need to set skip back to False so that you don't skip next item.
Finally, if it's not either case above, you add the value up to sum
You can use the zip function to loop the values in pairs:
def special_sum(numbers):
s = 0
for (prev, current) in zip([None] + numbers[:-1], numbers):
if prev != 13 and current != 13:
s += current
return s
or you can do a oneliner:
def special_sum(numbers):
return sum(current for (prev, current) in zip([None] + numbers[:-1], numbers)
if prev != 13 and current != 13)
You can also use iterators:
from itertools import izip, chain
def special_sum(numbers):
return sum(current for (prev, current) in izip(chain([None], numbers), numbers)
if prev != 13 and current != 13)
(the first list in the izip is longer than the second, zip and izip ignore the extra values).
Use a while loop to walk through the list, incrementing i manually. On each iteration, if you encounter a 13, increment i twice; otherwise, add the value to a running sum and increment i once.
def skip13s(l):
i = 0
s = 0
while (i < len(l)):
if l[i] == 13:
i += 1
else:
s += l[i]
i += 1
return s
Some FP-style :)
def add_but_skip_13_and_next(acc, x):
prev, sum_ = acc
if prev != 13 and x != 13:
sum_ += x
return x, sum_
filter_and_sum = lambda l: reduce(add_but_skip_13_and_next, l, (0,0))[1]
>>> print filter_and_sum([13,13,1,4])
4
>>> print filter_and_sum([1,2,13,5,13,13,-9,13,13,13,13,13,1,1])
4
This code works for any iterator, even it not provide the random access (direct indexing) - socket for example :)
Oneliner :)
>>> filter_and_sum = lambda l: reduce(
... lambda acc, x: (x, acc[1] + (x if x != 13 and acc[0] != 13 else 0)),
... l, (0,0))[1]
>>> print filter_and_sum([1,2,13,5,13,13,-9,13,13,13,13,13,1,1])
4
I think this is the most compact solution:
def triskaidekaphobicSum(sequence):
return sum(sequence[i] for i in range(len(sequence))
if sequence[i] != 13 and (i == 0 or sequence[i-1] != 13))
This uses the builtin sum() function on a generator expression. The generator produces all the elements in the sequence as long as they are not 13, or immediately following a 13. The extra "or" condition is to handle the first item in the sequence (which has no previous item).
You can use while loop to treat multiple 13.
def sum13(lis):
while pos(lis):
if pos(lis) == len(lis) - 1:
lis = lis[:pos(lis)]
else:
lis = lis[:pos(lis)]+lis[pos(lis)+1:]
return sum(lis)
def skipAndAddFun(inputVal, skipList):
sum = 0
for i in inputVal:
if not i in skipList:
sum += i
return sum
Usage:
skipAndAddFun([1,2,13,5,1], [13, 5])
4
This simple function will be a generic solution for your question.