Turn a function into a generator function with no parameters - python

I am new to Python and having trouble wrapping my head around converting the following function into a generator function that doesn't take any parameters (without importing itertools). The idea is to generate an infinite look-and-say sequence starting with 2, 12, 1112, 3112...and so on. As it is now, the sequence is working, but I'm not quite sure where I need to change my returns to yields or how to do it without taking a parameter (getting rid of (n)). Any help would be appreciated!
def count_sequence(n):
if (n == 1):
return "2"
if (n == 2):
return "12"
x = "12"
for i in range (3, n + 1):
x += "$"
l = len(x)
count = 1
temp = ""
for y in range(1, l):
if (x[y] != x[y - 1]):
temp += str(count + 0)
temp += x[y - 1]
count = 1
else:
count += 1
x = temp
return x

Start with a wrapper around your function:
def my_generator():
value = 1
while True:
result = count_sequence(value)
yield result
value += 1
then in-line the definition of count_sequence, replacing return with yield and n with value. (In this case, I skip a step and also yield the return value directly, rathe than assigning it to result first then using yield result once at the bottom of the while loop.)
def my_generator():
value = 1
while True:
if value == 1:
yield "2"
elif value == 2:
yield "12"
else:
x = "12"
for i in range(3, value+1):
x += "$"
l = len(x)
count = 1
temp = ""
for y in range(1, l):
if x[y] != x[y-1]:
temp += str(count)
temp += x[y-1]
count = 1
else:
count += 1
x = temp
yield x
value += 1
Then you can simplify it a little, for example pulling the first two yield expression statements out of the loop and getting rid of the if statement.
def my_generator():
yield "2"
yield "12"
value = 3
while True:
x = "12"
for i in range(3, value+1):
x += "$"
l = len(x)
count = 1
temp = ""
for y in range(1, l):
if x[y] != x[y-1]:
temp += str(count)
temp += x[y-1]
count = 1
else:
count += 1
x = temp
yield x
value += 1

I assume your count_sequence() method is called like this:
count_sequence(1)
count_sequence(2)
count_sequence(3)
...
As a generator the function would look like this:
def count_sequence():
yield "2"
yield "12"
x = "12"
n = 2
while True:
n += 1
for i in range (3, n + 1):
x += "$"
l = len(x)
count = 1
temp = ""
for y in range(1, l):
if (x[y] != x[y - 1]):
temp += str(count + 0)
temp += x[y - 1]
count = 1
else:
count += 1
x = temp
yield x
n is a variable now tracked within the generator function.
Call the generator with:
my_generator = count_sequence()
next(my_generator)
next(my_generator)
next(my_generator)
...

Related

How to use searching algorithm like jump search or fibonacci search on list that has nested list?

i have a problem with finding the solution to make searching algorithm on a list that have various type of item, especially with nested list.
def fib(n):
if n < 1:
return 1
elif n == 1 :
return 1
else:
return fib(n-1) + fib(n-2)
def fibonaccisearch(arr,x,q):
p = 0
p += q
n = 0
while p > 0:
while fib(n) < len(arr) and p != 0:
n = n + 1
offset = -1
while (fib(n) > 1) and p > 0:
i = min(offset + fib(n-2), len(arr) - 1)
if type(arr[i]) == list:
countingx(arr[i])
z = stringdata2.count(x)
print(f"{x} found1 at index:", i,"kolom ke -",fibonaccisearch(arr[i],x,z))
arr[i] = 'this is x'
p -= 1
elif (x > arr[i]):
n = n-1
offset = i
elif (x < arr[i]):
n = n-2
elif (x == arr[i]):
print(arr[i])
print(f"{x} found2 at index :", i)
p -= 1
arr[i] = 'this is x'
while p == 1:
z = 0
z += i
while z > -1:
if arr[z] != x:
z -= 1
else:
print(f"{x} found3 at index :", z)
arr[z] = 'this is X'
z = -1
p -= 1
else:
print()
if (fib(n-1) and arr[offset + 1] == x):
p -= 1
return offset + 1
return -1
def countingx(data):
global stringdata2
global list1
stringdata1 = []
stringdata2 = []
list1 = []
stringdata1.extend(data)
while True:
if len(stringdata1) == 0:
break
elif type(stringdata1[0]) == str:
stringdata2.append(stringdata1[0])
stringdata1.pop(0)
else:
list1.extend(stringdata1[0])
stringdata1.pop(0)
stringdata1.extend(list1)
list1.clear()
arr = ['b','a', ['c', 'a'], 'd', ['a', 'e', 'c'], 'a']
x = 'a'
countingx(arr)
print(stringdata2)
q = stringdata2.count(x)
print(arr)
fibonaccisearch(arr,x,q)
this is my code i dont know whats wrong with it i completely lost my mind trying to find the solution, i try to modified it the closes i find is, the code find the way to find the 'a' in the index 2 column 1, index 4 column 0, and index 5 but skips the 'a' in the index 1, i tried to fix it but it still dont works.

Not able to append List to List Python ( N-Queen Problem )

I'm a beginner in Leetcode & Python and I tried to solve the N-Queen Problem. Below is the solution in Python which passed submission
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
def isSafe(row, x_col):
# check row
for x in range(n):
if placed[row][x] is True and x != x_col:
return False
# check column
for x in range(n):
if placed[x][x_col] is True and x != row:
return False
# check diagnol
i, j = row, x_col
while i > 0 and j < n - 1:
if placed[i - 1][j + 1] is True:
return False
i -= 1
j += 1
i, j = row, x_col
while i < n - 1 and j < 0:
if placed[i + 1][j - 1] is True:
return False
i -= 1
j += 1
# #check offdiagnol
i, j = row, x_col
while i > 0 and j > 0:
if placed[i - 1][j - 1] is True:
return False
i -= 1
j -= 1
i, j = row, x_col
while i < n-1 and j < n-1:
if placed[i][j] is True:
return False
i += 1
j += 1
return True
def process(currconfig, matrix, n, row):
#base condition
if row == n:
curr = []
for x in range(n):
curr.append(currconfig[x])
totalconfig.append(currconfig)
return
for x_col in range(n):
if isSafe(row, x_col):
path = x_col * '.' + 'Q' + (n - x_col - 1) * '.'
currconfig.append(path)
matrix[row][x_col] = True
#process recursively called
process(currconfig, matrix, n, row + 1)
currconfig.pop()
matrix[row][x_col] = False
configuration = []
totalconfig = []
placed = [[False for _ in range(n)] for _ in range(n)]
process(configuration, placed, n, 0)
return totalconfig
a = Solution()
print(a.solveNQueens(4))
I only have a doubt about the base condition of the process function. In the process function's base condition I initially did this and got empty Lists appended to totalconfig no matter what, CASE I:
def process(currconfig, matrix, n, row):
if row == n:
totalconfig.append(currconfig)
return
so spent a few hours trying to get around the problem and weirdly enough for me this worked, CASE II :
def process(currconfig, matrix, n, row):
if row == n:
curr = []
for x in range(n):
curr.append(currconfig[x])
totalconfig.append(currconfig)
return
I don't get why the CASE I did not work but CASE II worked. What am I missing...How is "currconfig" in CASE I different from "curr" in CASE II
it looks like you are not using functions (methods) in classes correctly.
Basically, the first argument is the self which refers to itself. In your case, currconfig is self.
So presumably the function should have looked like this:
def process(self, currconfig, matrix, n, row): # <---added self
if row == n:
totalconfig.append(currconfig)
return

Is there any way to fix my python code being stuck?

primeNum = []
def isPrime(y):
i = 2
while(i < number):
if number % i == 0:
return False
i = i + 1
return True
def printNum(x):
k = 2
while k <= x:
if isPrime(k):
primeNum.append(k)
k = k + 1
printNum(number)
numOfPrime = len(primeNum)
sum = 0
j= 0
while j < numOfPrime:
sum += primeNum[j]
j = j + 1
print(sum)
When I run the code it just gets stuck on the input. I have tried everything but it doesn't give me an error unless I press CTRL + C then it will tell me Traceback.....
the line k = k + 1 in the function printNum was out of the while loop , making it an infinite loop,same goes for i = i + 1 in isPrime and j = j + 1 in the last loop, so i fixed it for you
additionally, the function isPrime had some bugs
it referred to number instead of y in a few places so I fixed that too
this should do the trick:
primeNum = []
number=10
def isPrime(y):
i = 2
while (i < y):
if y % i == 0:
return False
i = i + 1
return True
def printNum(x):
k = 2
while k <= x:
if isPrime(k):
primeNum.append(k)
k = k + 1
printNum(number)
numOfPrime = len(primeNum)
sum = 0
j = 0
while j < numOfPrime:
sum += primeNum[j]
j = j + 1
print(sum)
print("end")

Collatz conjecture sequence

The Collatz conjecture
what i am trying to do:
Write a function called collatz_sequence that takes a starting integer and returns the sequence of integers, including the starting point, for that number. Return the sequence in the form of a list. Create your function so that if the user inputs any integer less than 1, it returns the empty list [].
background on collatz conjecture:
Take any natural number n. If n is even, divide it by 2 to get n / 2, if n is odd multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
What I have so far:
def collatz_sequence(x):
seq = [x]
if x < 1:
return []
while x > 1:
if x % 2 == 0:
x= x/2
else:
x= 3*x+1
return seq
When I run this with a number less than 1 i get the empty set which is right. But when i run it with a number above 1 I only get that number i.e. collatz_sequence(6) returns [6]. I need this to return the whole sequence of numbers so 6 should return 6,3,10,5,16,8,4,2,1 in a list.
You forgot to append the x values to the seq list:
def collatz_sequence(x):
seq = [x]
if x < 1:
return []
while x > 1:
if x % 2 == 0:
x = x / 2
else:
x = 3 * x + 1
seq.append(x) # Added line
return seq
Verification:
~/tmp$ python collatz.py
[6, 3, 10, 5, 16, 8, 4, 2, 1]
def collatz_sequence(x):
seq = [x]
while seq[-1] > 1:
if x % 2 == 0:
seq.append(x/2)
else:
seq.append(3*x+1)
x = seq[-1]
return seq
Here's some code that produces what you're looking for. The check for 1 is built into while statement, and it iteratively appends to the list seq.
>>> collatz_sequence(6)
[6, 3, 10, 5, 16, 8, 4, 2, 1]
Note, this is going to be very slow for large lists of numbers. A cache won't solve the speed issue, and you won't be able to use this in a brute-force solution of the project euler problem, it will take forever (as it does every calculation, every single iteration.)
Here's another way of doing it:
while True:
x=int(input('ENTER NO.:'))
print ('----------------')
while x>0:
if x%2==0:
x = x/2
elif x>1:
x = 3*x + 1
else:
break
print (x)
This will ask the user for a number again and again to be put in it until he quits
def collatz(x):
while x !=1:
print(int(x))
if x%2 == 0:
x = x/2
else:
x = 3*x+1
this is what i propose..
seq = []
x = (int(input("Add number:")))
if (x != 1):
print ("Number can't be 1")
while x > 1:
if x % 2 == 0:
x=x/2
else:
x = 3 * x + 1
seq.append (x)
print seq
This gives all the steps of a single number. It has worked with a 50-digit number in 0,3 second.
collatz = []
def collatz_sequence(x):
while x != 1:
if x % 2 == 0:
x /= 2
else:
x = (3*x + 1)/2
collatz.append(int(x))
print(collatz)
collatz_sequence()
Recursion:
def collatz(n):
if n == 1: return [n]
elif n % 2 == 0: return [n] + collatz(int(n/2))
else: return [n] + collatz(n*3+1)
print(collatz(27))
steps=0
c0 = int(input("enter the value of c0="))
while c0>1:
if c0 % 2 ==0 :
c0 = c0/2
print(int(c0))
steps +=1
else:
c0 = (3 * c0) + 1
print(int(c0))
steps +=1
print("steps= ", steps)
import numpy as np
from matplotlib.pyplot import step, xlim, ylim, show
def collatz_sequence(N):
seq = [N]
m = 0
maxN = 0
while seq[-1] > 1:
if N % 2 == 0:
k = N//2
seq.append(N//2)
if k > maxN:
maxN = k
else:
k = 3*N+1
seq.append(3*N+1)
if k > maxN:
maxN = k
N = seq[-1]
m = m + 1
print(seq)
x = np.arange(0, m+1)
y = np.array(seq)
xlim(0, m+1)
ylim(0, maxN*1.1)
step(x, y)
show()
def collatz_exec():
print('Enter an Integer')
N = int(input())
collatz_sequence(N)
This is how you can use it:
>>> from collatz_sequence import *
>>> collatz_exec()
Enter an Integer
21
[21, 64, 32, 16, 8, 4, 2, 1]
And a plot that shows the sequence:
seq = []
def collatz_sequence(x):
global seq
seq.append(x)
if x == 1:
return
if (x % 2) == 0:
collatz_sequence(x / 2)
else:
collatz_sequence((x * 3) + 1)
collatz_sequence(217)
print seq
def collataz(number):
while number > 1:
if number % 2 == 0 :
number = number //2
print(number)
elif number % 2 ==1 :
number = 3 * number + 1
print(number)
if number == 1 :
break
print('enter any number...!')
number=int(input())
collataz(number)

Bubble sort error

I am trying to write and optimized bubble sort, but my code does not actually sort right through the list. Any suggestions as to why it is stopping early?
#!/usr/bin/env python
from random import *
import time
def bubble(lst):
counter = 0
n = len(lst)
while n > 0:
temp = 0
for i in range(1, n - 1):
if lst[i] > lst[i + 1]:
lst[i], lst[i + 1] = lst[i + 1], lst[i] # swap
temp = i
counter += 1 # to compare the speed to the
# unoptimized bubble sort
n = temp
return counter
def main():
lst = range(10)
shuffle(lst)
print lst
start = time.time()
counter = bubble(lst)
print lst
end = time.time()
print counter
print "time", end - start
main()
This should fix it:
def bubble(lst):
counter = 0
n = len(lst)
while n > 0:
temp = 0
for i in range(0,n-1): # <- first element is at position 0
if lst[i] > lst[i+1]:
lst[i],lst[i+1] = lst[i+1],lst[i] #swap
temp = i+1 # <- the last swapped position
counter += 1
n = temp
return counter

Categories