Python Recursively check for repeats - python

I have an assignment I've been stuck on for a couple days now. I have to recursively figure out if a list has repeats but I cannot use any loops or built in functions besides len(). I'm also not allowed to use the 'in' function. Returns True if list L has repeats, False otherwise. This is what I've been able to figure out:
def has_repeats(L):
if len(L) <= 1:
return False
elif L[0] == L[1]:
return True
else: return has_repeats(L[0] + L[2:])
But the problem with that is it's only comparing the first element to the rest, instead of each element to the rest. I can't figure out how to do that without a running counter or something. Any suggestions?

You almost have it. Along with checking the first element with the rest of the list, you also need to check the second the same way:
def has_repeats(L):
if len(L) <= 1:
return False
if L[0] == L[1]:
return True
if has_repeats([L[0]] + L[2:]):
return True
if has_repeats(L[1:]):
return True
return False
You can also compact this into the following representation:
def has_repeats(L):
return len(L)>1 and L[0]==L[1] or has_repeats([L[0]]+L[2:]) or has_repeats(L[1:])

Use a helper function:
def helper(ele, rest):
if not rest:
return False
return ele == rest[0] or helper(ele, l[1:])
def has_repeats(l):
if not l:
return False
return helper(l[0], l[1:]) or has_repeats(l[1:])

Related

how to check if a list is a consecutive number in python?

How to write python code that let the computer know if the list is a right sequence and the position doesn't matter, it will return true, otherwise it return false.
below are some of my example, I really don't know how to start
b=[1,2,3,4,5] #return true
b=[1,2,2,1,3] # return false
b=[2,3,1,5,4] #return true
b=[2,4,6,4,3] # return false
sort function is O(nlogn), we can use for loop which is O(n):
def check_seq(in_list):
now_ele = set()
min_ele = max_ele = in_list[0]
for i in in_list:
if i in now_ele:
return False
min_ele = min(i, min_ele)
max_ele = max(i, max_ele)
now_ele.add(i)
if max_ele-min_ele+1 == len(in_list):
return True
return False
Create a set and one to compare with -- based on minimum and maximum:
isRightSequence = set(range(min(b), max(b)+1)) == set(b)
This question is quite simple and can be solved a few ways.
The conditional approach - if there is a number that is bigger than the length of the list, it automatically cannot be a sequence because there can only be numbers from 1-n where n is the size of the list. Also, you have to check if there are any duplicates in the list as this cannot be possible either. If none of these conditions occur, it should return true
Using dictionary - go through the entire list and add it as a key to a dictionary. Afterwards, simply loop through numbers 1-n where n is the length of the list and check if they are keys in the dictionary, if one of them isn't, return false. If all of them are, return true.
Both of these are quite simply approaches and you should be able to implement them yourselves. However, this is one implementation for both.
1.
def solve(list1):
seen = {}
for i in list1:
if i > len(list1):
return False
if i in seen:
return False
seen[i] = True
return False
def solve(list1):
seen = {}
for i in list1:
seen[i] = True
for i in range (1, len(list1)+1):
if i not in seen:
return False
return True
This solution needs O(n) runtime and O(n) space
def is_consecutive(l: list[int]):
if not l:
return False
low = min(l)
high = max(l)
# Bounds Check
if high - low != len(l) - 1:
return False
# Test all indices exist
test_vec = [False] * len(l) # O(n)
for i in range(len(l)):
test_vec[l[i] - low] = True
return all(test_vec)
assert is_consecutive(range(10))
assert is_consecutive([-1, 1,0])
assert not is_consecutive([1,1])
assert not is_consecutive([1,2,4,6,5])

Given a list of ints, return True if the array contains a 3 next to a 3 somewhere

I want to creates a function that returns True if a list contains two consecutive 3 and false if not.
when using this code it doesn't work:
def has_33(x):
for i in range(len(x)-1):
if x[i:i+2] == [3,3]:
return True
else:
return False
But with this it works:
def has_33(x):
for i in range(len(x)-1):
if x[i:i+2] == [3,3]:
return True
return False
For me it's the same thing can you explain to me why plz
In the top code you only check the first 2 indices. In the bottom code you're checking the whole array. Here's why:
In the top code you have an else condition, which means that on the first iteration either the if-condition is true (i.e. the first and the second element of the list are 3) or you will go into the else condition. In either case, you're hitting a return statement after checking just the first 2 elements.
In the bottom code, you only return if you find something, or once you've finished the whole loop
I've added some print statements to both your functions. You can run this code and see the output to help you understand what's happening:
def has_33_incorrect(x):
print("\nRunning the incorrect function")
for i in range(len(x)-1):
print("Checking indices {} and {}".format(i, i+1))
if x[i:i+2] == [3,3]:
print("indices contain 2 consecutive 3's. Returning true")
return True
else:
print("indices don't contain 2 consecutive 3's. Returning false")
return False
def has_33_correct(x):
print("\nRunning the correct function")
for i in range(len(x)-1):
print("Checking indices {} and {}".format(i, i+1))
if x[i:i+2] == [3,3]:
print("indices contain 2 consecutive 3's. Returning true")
return True
print("Did not find consecutive 3's. Returning false")
return False
list = [1, 2, 4, 5, 6, 3, 3, 2, 5]
has_33_incorrect(list)
has_33_correct(list)
One more solution with any python builtin:
def has_33(l):
return any(l[i+1] == l[i] == 3 for i in range(len(l) - 1))
This code works since if the if condition is false it doesn't return anything, yet in the first function you showed it returns false and exits the function. You shouldn't return anything until you find that its true somewhere, only after you've iterate over the entire list then and only then should you return False
def has_33(x):
for i in range(len(x)-1):
if x[i:i+2] == [3,3]:
return True
return False
You can iterate over the list directly.
def has_33(x):
current = None
for item in x:
if current == item == 3:
return True
current = item
return False
If you ever find both current and item are equal to 3, you can return True immediately. Otherwise, you will return False when the loop exits naturally.
If you loop through adjacent entries at the same time, you can compare:
def adjacent_threes(x):
for a, b, in zip(x[:-1], x[1:]):
if a == b == 3:
return True
return False
Try this ,it's simple:
def has_33(nums):
for i,num in enumerate(nums):
if nums[i]==3 and nums[i+1]==3:
return True
return False
python code as follows :
def has_33(nums):
my_list = []
y =len(nums)-1
for x in range(0,y) :
if nums[x] == 3 and nums[x+1] == 3:
my_list.append(1)
else :
my_list.append(0)
print(my_list)
if 1 in my_list:
return True
else :
return False

How to write this iterative function to be recursive?

I need to write this iterative function to do the same thing but it must be recursive.
def task1(string: str):
for i in range(len(string)):
if string[i] != string[len(string) - i - 1]:
return False
return True
This is what i tried but it does not work.
def task1_recursion(string: str):
print(string)
if len(string) > 1:
if string[0] == task1_recursion(string[1::1]):
return True
else:
return False
else:
return string
My code seems to one the last recursion return string "" and that makes it to return False.
Just check the tip and the tail, continue with the string without them:
def task1_recursion(string: str):
# recursion base condition (exit condition)
if len(string) <= 1:
return True
# unpack values
first, *_, last = string
# check if they are different
if first != last:
return False
# if not continue checking the remaining string
return task1_recursion(string[1:-1])
If I understand correctly you want to check if a string is symmetric with the code in task1. My solution is below:
def fct(s: str, i: int):
if len(s) <= 1 or i == len(s):
return True
return s[i] == s[len(s) - 1 - i] and fct(s, i + 1)
I tested and fct produces the same result as task1. It needs an additional parameter for the index though. But you can wrap it inside another function if you want the parameter to include only the input string. i is always set to 0 when you call the function, e.g. fct("ABCCBA", 0).

Recursive function seems to be fine, but won't return False

So the function should check if a list is symmetric; if the list is empty or has one integer, it is symmetric.
Otherwise the function should check if the first and last integers are the same and go on recursively. If not, return False.
The code seems right, but I can't figure out what's wrong and why it won't return False.
def symm(lst):
t = len(lst)
if t == 0 or 1:
return True
if t>1:
if lst[0] == lst[-1]:
return symm(lst[1:-2])
else:
return False
print symm([6,6,6,6,7])
if t == 0 or 1: is always true since this is read as if (t == 0) or (1): and 1 is always going to be true.
Change to: if t in [0,1]: or simply if len(lst) < 2:

How can I return false if more than one number while ignoring "0"'s?

This is a function in a greater a program that solves a sudoku puzzle. At this point, I would like the function to return false if there is more then 1 occurrence of a number unless the number is zero. What do am I missing to achieve this?
L is a list of numbers
l =[1,0,0,2,3,0,0,8,0]
def alldifferent1D(l):
for i in range(len(l)):
if l.count(l[i])>1 and l[i] != 0: #does this do it?
return False
return True
Assuming the list is length 9, you can ignore the inefficiency of using count here (Using a helper datastructure - Counter etc probably takes longer than running .count() a few times). You can write the expression to say they are all different more naturally as:
def alldifferent1D(L):
return all(L.count(x) <= 1 for x in L if x != 0)
This also saves calling count() for all the 0's
>>> from collections import counter
>>> def all_different(xs):
... return len(set(Counter(filter(None, xs)).values()) - set([1])) == 0
Tests:
>>> all_different([])
True
>>> all_different([0,0,0])
True
>>> all_different([0,0,1,2,3])
True
>>> all_different([1])
True
>>> all_different([1,2])
True
>>> all_different([0,2,0,1,2,3])
False
>>> all_different([2,2])
False
>>> all_different([1,2,3,2,2,3])
False
So we can break this down into two problems:
Getting rid of the zeros, since we don't care about them.
Checking if there are any duplicate numbers.
Striping the zeros is easy enough:
filter(lambda a: a != 0, x)
And we can check for differences in a set (which has only one of each element) and a list
if len(x) == len(set(x)):
return True
return False
Making these into functions we have:
def remove_zeros(x):
return filter(lambda a: a != 0, x)
def duplicates(x):
if len(x) == len(set(x)):
return True
return False
def alldifferent1D(x):
return duplicates(remove_zeros(x))
One way to avoid searching for every entry in every position is to:
flags = (len(l)+1)*[False];
for cell in l:
if cell>0:
if flags[cell]:
return False
flags[cell] = True
return True
The flags list has a True at index k if the value k has been seen before in the list.
I'm sure you could speed this up with list comprehension and an all() or any() test, but this worked well enough for me.
PS: The first intro didn't survive my edit, but this is from a Sudoku solver I wrote years ago. (Python 2.4 or 2.5 iirc)

Categories