primitive calculator Second step, - python-3.x

I try to get the code right for the minimum required steps to get to n and the subsequent list, steps are multiply by 2 or 3, or add 1. It should be implemented through dynamic programming since using greedy choices for this one is unsafe. First I got the minimum steps required using the following code, it works just fine .
What I can't get is how do I go about unpacking that list, please I don't need answers, this is an assignment for an online class, hints or Help is Ok, so I try to maintain my moral integrity here.
def optimal_sequence(n):
sequence = [0]*(n+1)
for i in range(2, n+1):
if i % 3 == 0:
sequence[i] = sequence[(i // 3)]+1
if sequence[i] - sequence[i-1] <= 1:
sequence[i] = sequence[i]
else:
sequence[i] = sequence[i-1] + 1
elif i % 2 == 0:
sequence[i] = sequence[(i // 2)]+1
if sequence[i] - sequence[i-1] <= 1:
sequence[i] = sequence[i]
else:
sequence[i] = sequence[i-1] + 1
else:
sequence[i] = sequence[i-1] + 1
return sequence
while n >= 1:
seq.append(n)
if n % 3 == 0 and sequence[n] != sequence[n//3]: n = n // 3
elif n % 2 == 0 and sequence[n] != sequence[n//2]: n = n // 2
else: n = n-1
seq.reverse(); return seq
Here is how it maps the minimum number for let's say n = 28,
n = 28
print(optimal_sequence(n))
>>> [0, 0, 1, 1, 2, 3, 2, 3, 3, 2, 3, 4, 3, 4, 4, 4, 4, 5, 3, 4, 4, 4, 5, 6, 4, 5, 5, 3, 4]
Now, if you can help me see if the code is right for the first part, if it is, then what is wrong with the second part of it beginning with while.
Thanks

Related

Print the sum of the list of numbers. If the list is empty zero is printed also the element 7 and the element next to it won’t contribute to the sum

I tried solving this way but didn't work. This is a question from geeksforgeeks
arr = [1, 3, 4, 4, 7, 7]
def check(arr):
sums = sum(arr)
if arr == []:
sums = 0
else:
for i in range(len(arr)-1):
if arr[i] == 7:
sums = sums-arr[i]-arr[i+1]
else:
sums = sums
return sums
The question is somewhat vague in defining what "next to it" is, I assumed it's the succeeding element.
So I made a solution with a constraint that literal 7 and number subsequent to 7 are ignored in the summation of the sequence
def check(seq):
return sum(n for i, n in enumerate(seq) if n != 7 and seq[i - (i != 0)] != 7)
Checks that I did on the solution to match the requirements :
assert check([]) == 0, "Empty case"
assert check([1, 7, 8, 1]) == 2, "Exclude 7 and the number next to (8 is ignored) it"
assert check([1, 2, 3, 4, 5, 6, 7]) == 21, "Exclude 7"
assert check([7, 10]) == 0, "7 and 10 are ignored"
If you have an link to the original code challenge then posting it here would be helpful.
def realSum(mylist):
l1=0
v=[]
if len(mylist)>0:
for i in range(len(mylist)):
if mylist[i]==7:
if i not in v or v==[]:
if i!=len(mylist)-1:
v.append(i)
v.append(i+1)
else:
v.append(i)
for i in range(len(mylist)):
if i not in v:
l1+=mylist[i]
return l1
else:
return 0

Counting instances of N in an array using Recursion in Python

I would like to count the number of instances of a given number N in an array using recursion. For example, given:
array = [1, 2, 3, 1, 1, 4, 5, 2, 1, 8, 1]
and N = 1, the function should return 5.
This problem can be solved using the .counter attribute as shown here. However, I am looking to not use any in-built functions or attributes.
Here's my attempt to solve this using recursion but I get a count of 1 and not 5. What am I doing wrong?
def count_val(array, n, count=0):
if len(array) == 0:
return None
# Base Case
if len(array) == 1:
if array[0] == n:
count += 1
else:
count_val(array[1:], n, count)
if array[0] == n:
count += 1
return count
print(count_val2(array, 1))
1
I think for an empty array, the value should be 0 (len == 0 should be the base case), and, you don't need to have a count parameter if you just return the count, your function could be reduced to this:
def count_val(array, n):
if len(array) == 0:
return 0
return (array[0] == n) + count_val(array[1:], n)
array = [1, 2, 3, 1, 1, 4, 5, 2, 1, 8, 1]
print(count_val(array, 1))
Output:
5
You can have it as a one-liner as well (as suggested by #blhsing):
def count_val(array, n):
return len(array) and (array[0] == n) + count_val(array[1:], n)
What am I doing wrong?
The function you wrote will always keep only the last few characters, so after a while it will be [1, 8, 1], after that [8, 1] and after that [1], which returns 1. The array never contains just any of the other 1s.
An easy way to do this is to loop over all elements in a list and test if they are equal to N.
array = [1, 2, 3, 1, 1, 4, 5, 2, 1, 8, 1]
def count_val(array, n):
if len(array) == 0:
return 0
count=0
for i in array:
if i==n:
count += 1
return count
print(count_val(array, 1))
This returns 5.

How to iterate through a numbered list and every time value 1 appears start over?

Given a list:
list1 = [1,2,3,4,5,6,1,2,3,4,1,2,3,4,5,6,7]
While iterating though list1, every time the integer 1 is hit, start the loop over but increment it by 1.
Tried the two examples below but it only returns a list of 1's for the length of list1.
digit = []
i = 0
for num in list1:
num = i
if num != 1:
i += 1
digit.append(i)
elif num == 1:
digit.append(num)
digit = []
i = 0
for num in list1:
num = i
if num == 1:
digit.append(num)
continue
elif num != 1:
i += 1
digit.append(i)
digit
Looking to get something like the list below
digit = [1,1,1,1,1,1,2,2,2,2,3,3,3,3,3,3,3]
You're overthinking this. Initialise a variable to zero. Append it to a list at each iteration. Increment if the corresponding list value is 1.
values = []
i = 0
for l in list1:
if l == 1: # The check must come before appending. Can you explain why?
i += 1
values.append(i)
values
# [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3]
If you were to ask me for a pythonic solution to this problem, I'd suggest itertools.accumulate:
from itertools import accumulate
from operator import add
list(accumulate((int(x == 1) for x in list1), add))
# [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3]

What is wrong with the syntax for this insertion sort and bubble sort code?

I am currently taking discrete structures and algorithms and have to work with python for the first time.
I am having a little trouble with the syntax and having a problem with my bubble sort and insertion sort function printing
def insertion_sort(numbers):
numbers = [1, 5, 9, 3, 4, 6]
for index in range(1, len(numbers)):
value = numbers[index]
i = index - 1
while i >= 0:
if value < numbers[i]:
numbers[i+1] = numbers[i]
numbers[i] = value
i = i - 1
print(numbers)
else:
break
def bubble_sort(numbers):
for i in range(0, len(numbers) - 1, 1):
for j in range(0, len(numbers) - 1 - i, 1):
if numbers[j] < numbers[j + 1]:
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
numbers = [1, 5, 9, 3, 4, 6]
print(numbers)
You've defined two functions but never call them. Therefore, they are not getting executed.
You've defined your two functions such that they expect a numbers parameter. So you need to call them with a list of numbers as input. eg. insertion_sort([1, 5, 9, 3, 4, 6])
Your functions are not returning any value. So they are simply taking the numbers list parameter, and sorting it. In order to access the result from outside the function, you need to add return numbers at the end of each function.
All in all, your code should look something like this:
def insertion_sort(numbers):
for index in range(1, len(numbers)):
value = numbers[index]
i = index - 1
while i >= 0:
if value < numbers[i]:
numbers[i+1] = numbers[i]
numbers[i] = value
i = i - 1
else:
break
return numbers
def bubble_sort(numbers):
for i in range(0, len(numbers) - 1, 1):
for j in range(0, len(numbers) - 1 - i, 1):
if numbers[j] < numbers[j + 1]:
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
return numbers
numberstosort = [1, 5, 9, 3, 4, 6]
print(insertion_sort(numberstosort))
print(bubble_sort(numberstosort))
This will print the output of each function. Output:
[1, 3, 4, 5, 6, 9]
[9, 6, 5, 4, 3, 1]

Euler Project 111 Code to make shorter and shorter

I am solving Euler Project # 111 problem, and the code I wrote is running for too long. How can I write this shorter, simpler, faster?
The problem is the link
https://projecteuler.net/problem=111
result = 0
a = []
count_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
for i in range(1000000000, 9999999999):
if i == 2:
result += i
for j in range(1000000000, i):
if i % j == 0:
break
elif j == i -1:
a = list(str(i))
for k in range(len(count_number)):
if a.count(count_number[k]) == 1:
break
elif a.count(count_number[k] == count_number[k]):
result += i
del a
print(result)

Resources