Running multiline test cases - python

I have the following prompt:
We want you to calculate the sum of squares of given integers, excluding any negatives.
The first line of the input will be an integer N (1 <= N <= 100), indicating the number of test cases to follow.
Each of the test cases will consist of a line with an integer X (0 < X <= 100), followed by another line consisting of X number of space-separated integers Yn (-100 <= Yn <= 100).
For each test case, calculate the sum of squares of the integers, excluding any negatives, and print the calculated sum in the output.
Note: There should be no output until all the input has been received.
Note 2: Do not put blank lines between test cases solutions.
Note 3: Take input from standard input, and output to standard output.
Specific Rules for Python Solution:
Your source code must be a single file, containing at least a main function
Do not use any for loop, while loop, or any list / set / dictionary comprehension
I have written my square_sum function as:
def square_sum(arr):
if not arr:
return 0
value = arr[0]
if value < 0:
value = 0
return value**2 + square_sum(arr[1:])
square_sum([9, 6, -53, 32, 16])
However, I cannot figure out how to run the multiline test cases on my function and display the result in the aforementioned format. Interestingly, there can be any number of test cases so how do I add the capability to accommodate them? I would like some guidance in this part, thank you.

Assuming that this assignment is to see how to replace all iteration with recursion, and withholding all judgement on the wisdom of doing so, here is a sample solution.
You have already implemented the inner loop. My suggestion would be to add the parsing to that loop, since otherwise you have to either use map or replace it with another recursion.
def square_sum(lst):
if not lst:
return 0
value = int(lst[0])
if value < 0:
value = 0
return value**2 + square_sum(lst[1:])
The outer loop will need to read two lines: the first (discarded) line will contain the number of elements. The second line will contain the strings that you will pass to square_sum. To control the depth of your recursion, use the first line of the input, which tells you how many samples there will be:
def run(n):
count = int(input())
print(square_sum(input().split()))
if n > 1:
run(n - 1)
def main()
n = int(input())
run(n)
Your question asks for a main function. If you need to run it in your module, go ahead and do that:
main()

Related

How can I count sequences that meet these constraints?

I am trying to count permutations of a sequence of I and O symbols, representing e.g. people entering (I for "in") and leaving (O for "out") a room. For a given n many I symbols, there should be exactly as many O symbols, giving a total length of 2*n for the sequence. Also, at any point in a valid permutation, the number of O symbols must be less than or equal to the number of I symbols (since it is not possible for someone to leave the room when it is empty).
Additionally, I have some initial prefix of I and O symbols, representing people who previously entered or left the room. The output should only count sequences starting with that prefix.
For example, for n=1 and an initial state of '', the result should be 1 since the only valid sequence is IO; for n=3 and an initial state of II, the possible permutations are
IIIOOO
IIOIOO
IIOOIO
for a result of 3. (There are five ways for three people to enter and leave the room, but the other two involve the first person leaving immediately.)
I'm guessing the simplest way to solve this is using itertools.permutations. This is my code so far:
n=int(input()) ##actual length will be 2*n
string=input()
I_COUNT=string.count("I")
O_COUNT=string.count("O")
if string[0]!="I":
sys.exit()
if O_COUNT>I_COUNT:
sys.exit()
perms = [''.join(p) for p in permutations(string)]
print(perms)
the goal is to get the permutation for whatever is left out of the string and append it to the user's input, so how can I append user's input to the remaining length of the string and get the count for permutation?
#cache
def count_permutations(ins: int, outs: int):
# ins and outs are the remaining number of ins and outs to process
assert outs >= ins
if ins == 0 :
# Can do nothing but output "outs"
return 1
elif outs == ins:
# Your next output needs to be an I else you become unbalanced
return count_permutations(ins - 1, outs)
else:
# Your. next output can either be an I or an O
return count_permutations(ins - 1, outs) + count_permutations(ins, outs - 1)
If, say you have a total of 5 Is and 5 Os, and you've already output one I, then you want: count_permutations(4, 5).
I'm guessing the simplest way to solve this is using itertools.permutations
Sadly, this will not be very helpful. The problem is that itertools.permutations does not care about the value of the elements it's permuting; it treats them as all distinct regardless. So if you have 6 input elements, and ask for length-6 permutations, you will get 720 results, even if all the inputs are the same.
itertools.combinations has the opposite issue; it doesn't distinguish any elements. When it selects some elements, it only puts those elements in the order they initially appeared. So if you have 6 input elements and ask for length-6 combinations, you will get 1 result - the original sequence.
Presumably what you wanted to do is generate all the distinct ways of arranging the Is and Os, then take out the invalid ones, then count what remains. This is possible, and the itertools library can help with the first step, but it is not straightforward.
It will be simpler to use a recursive algorithm directly. The general approach is as follows:
At any given time, we care about how many people are in the room and how many people must still enter. To handle the prefix, we simply count how many people are in the room right now, and subtract that from the total number of people in order to determine how many must still enter. I leave the input handling as an exercise.
To determine that count, we count up the ways that involve the next action being I (someone comes in), plus the ways that involve the next action being O (someone leaves).
If everyone has entered, there is only one way forward: everyone must leave, one at a time. This is a base case.
Otherwise, it is definitely possible for someone to come in. We recursively count the ways for everyone else to enter after that; in the recursive call, there is one more person in the room, and one fewer person who must still enter.
If there are still people who have to enter, and there is also someone in the room right now, then it is also possible for someone to leave first. We recursively count the ways for others to enter after that; in the recursive call, there is one fewer person in the room, and the same number who must still enter.
This translates into code fairly directly:
def ways_to_enter(currently_in, waiting):
if waiting == 0:
return 1
result = ways_to_enter(currently_in + 1, waiting - 1)
if currently_in > 0:
result += ways_to_enter(currently_in - 1, waiting)
return result
Some testing:
>>> ways_to_enter(0, 1) # n = 1, prefix = ''
1
>>> ways_to_enter(2, 1) # n = 3, prefix = 'II'; OR e.g. n = 4, prefix = 'IIOI'
3
>>> ways_to_enter(0, 3) # n = 3, prefix = ''
5
>>> ways_to_enter(0, 14) # takes less than a second on my machine
2674440
We can improve the performance for larger values by decorating the function with functools.cache (lru_cache prior to 3.9), which will memoize results of the previous recursive calls. The more purpose-built approach is to use dynamic programming techniques: in this case, we would initialize 2-dimensional storage for the results of ways_to_enter(x, y), and compute those values one at a time, in such a way that the values needed for the "recursive calls" have already been done earlier in the process.
That direct approach would look something like:
def ways_to_enter(currently_in, waiting):
# initialize storage
results = [[0] * currently_in for _ in waiting]
# We will iterate with `waiting` as the major axis.
for w, row in enumerate(results):
for c, column in enumerate(currently_in):
if w == 0:
value = 1
else:
value = results[w - 1][c + 1]
if c > 0:
value += results[w][c - 1]
results[w][c] = value
return results[-1][-1]
The product() function from itertools will allow you to generate all the possible sequences of 'I' and 'O' for a given length.
From that list, you can filter by the sequences that start with the user-supplied start_seq.
From that list, you can filter by the sequences that are valid, given your rules of the number and order of the 'I's and 'O's:
from itertools import product
def is_valid(seq):
'''Evaluates a sequence I's and O's following the rules that:
- there cannot be more outs than ins
- the ins and outs must be balanced
'''
_in, _out = 0, 0
for x in seq:
if x == 'I':
_in += 1
else:
_out += 1
if (_out > _in) or (_in > len(seq)/2):
return False
return True
# User inputs...
start_seq = 'II'
assert start_seq[0] != 'O', 'Starting sequence cannot start with an OUT.'
n = 3
total_len = n*2
assert len(start_seq) < total_len, 'Starting sequence is at least as big as total number, nothing to iterate.'
# Calculate all possible sequences that are total_len long, as tuples of 'I' and 'O'
seq_tuples = product('IO', repeat=total_len)
# Convert tuples to strings, e.g., `('I', 'O', 'I')` to `'IOI'`
sequences = [''.join(seq_tpl) for seq_tpl in seq_tuples]
# Filter for sequences that start correctly
sequences = [seq for seq in sequences if seq.startswith(start_seq)]
# Filter for valid sequences
sequences = [seq for seq in sequences if is_valid(seq)]
print(sequences)
and I get:
['IIIOOO', 'IIOIOO', 'IIOOIO']
Not very elegant perhaps but this certainly seems to fulfil the brief:
from itertools import permutations
def isvalid(start, p):
for c1, c2 in zip(start, p):
if c1 != c2:
return 0
n = 0
for c in p:
if c == 'O':
if (n := n - 1) < 0:
return 0
else:
n += 1
return 1
def calc(n, i):
s = i + 'I' * (n - i.count('I'))
s += 'O' * (n * 2 - len(s))
return sum(isvalid(i, p) for p in set(permutations(s)))
print(calc(3, 'II'))
print(calc(3, 'IO'))
print(calc(3, 'I'))
print(calc(3, ''))
Output:
3
2
5
5
def solve(string,n):
countI =string.count('I')
if countI==n:
return 1
countO=string.count('O')
if countO > countI:
return 0
k= solve(string + 'O',n)
h= solve(string + 'I',n)
return k+h
n= int(input())
string=input()
print(solve(string,n))
This is a dynamic programming problem.
Given the number of in and out operations remaining, we do one of the following:
If we're out of either ins or outs, we can only use operations of the other type. There is only one possible assignment.
If we have an equal number of ins or outs, we must use an in operation according to the constraints of the problem.
Finally, if we have more ins than outs, we can perform either operation. The answer, then, is the sum of the number of sequences if we choose to use an in operation plus the number of sequences if we choose to use an out operation.
This runs in O(n^2) time, although in practice the following code snippet can be made faster using a 2D-list rather than the cache annotation (I've used #cache in this case to make the recurrence easier to understand).
from functools import cache
#cache
def find_permutation_count(in_remaining, out_remaining):
if in_remaining == 0 or out_remaining == 0:
return 1
elif in_remaining == out_remaining:
return find_permutation_count(in_remaining - 1, out_remaining)
else:
return find_permutation_count(in_remaining - 1, out_remaining) + find_permutation_count(in_remaining, out_remaining - 1)
print(find_permutation_count(3, 3)) # prints 5
The number of such permutations of length 2n is given by the n'th Catalan number. Wikipedia gives a formula for Catalan numbers in terms of central binomial coefficients:
from math import comb
def count_permutations(n):
return comb(2*n,n) // (n+1)
for i in range(1,10):
print(i, count_permutations(i))
# 1 1
# 2 2
# 3 5
# 4 14
# 5 42
# 6 132
# 7 429
# 8 1430
# 9 4862

How can I optimize my code to print amicable numbers?

I have tried this following code and it takes a lot of time when I set lower = 0 and upper = 10000
def sumPdivisors(n):
'''This function returns the sum of proper divisors of a number'''
lst = []
for i in range(1,n//2+1):
if n%i == 0:
lst.append(i)
return(sum(lst))
lower = int(input("Enter the lower value of range: "))
upper = int(input("Enter the upper value of range: "))
lst = []
for i in range(lower, upper+1):
if i == 0:
continue
else:
for j in range(i, upper):
if i!=j and sumPdivisors(i) == j and sumPdivisors(j) == i:
lst.append((i,j))
break
print(lst)
There are two things that you could do here.
Memoization
There's already a great explanation of what memoization is elsewhere on this site [link], but here's how it's relevant to your problem:
sumPdivisors is called very frequently in the for-loop at the bottom of your code snippet. For really large inputs n, it will take a long time to run.
sumPdivisors is called with the same input n multiple times.
You can speed things up by saving the result of calling sumPdivisors on different inputs somehow, like in a dictionary that maps integers to the resulting output when you call sumPdivisors with that corresponding integer. This is kind of what memoization is. You're precomputing the possible outputs of sumPdivisors and storing them for later. Read the link for a more in-depth explanation.
Don't add the numbers in sumPdivisors to a list
You can just add these numbers as you iterate instead of appending them to a list, then summing them. This change won't have as great of an impact as adding memoization to your code.

How to find the product of the odd-indexed values in a list

I am able to solve this problem up the the 'even' part but I'm getting stuck in the odd part.
You will be given an array of n numbers. Your task is to first reverse the array (first number becomes last, 2nd number becomes 2nd from the last and so on) and then print the sum of the numbers at even indices and print the product of the numbers at odd indices.
Input
First line contains single integer N: number of elements
followed by N different integers separated by spaces
Output
Two space separated integers representing sum of the numbers at even places and the product of the numbers at odd places.
My code so far:
n = int(input())
arr = [int(x) for x in input().split()]
arr.reverse()
for ele in arr:
print(ele, end=" ")
print()
sum = 0
count = 1
while count <= n:
if count % 2 == 0:
sum += count
count += 1
print(sum)
There's a couple of issues in the code you've supplied which I'll address first:
Firstly, you need to be clear about what is meant by odd and even indices. In some languages (Matlab) for example, the first element of an array is index position 1. In Python and Java it's 0, so, whilst your example has assumed 1, it probably should be 0 unless otherwise specified.
Second, in your line sum+=count you're summing the index positions, not the index values, so that isn't what your question is asking for.
Last point on your code is that you've used sum as a variable name. Whilst that works, sum is also a Python keyword and you should avoid using them as variable names as if you later want to use the sum function, you will get the error TypeError: 'int' object is not callable because you've redefined the sum function to be an integer.
To the answer:
Considering the above, this provides the answer to part 1 by fixing your code:
total = 0
count = 0
while count < n:
if count % 2 == 0:
total += arr[count]
count += 1
print(total)
It's worth noting that as you're looking for even numbers, you could better write that as:
total = 0
count = 0
while count < n:
total += arr[count]
count += 2
print(total)
However, there are even easier ways to do this in much less code, and they involve list slicing. You can slice a list by specifying [start: end: step], so arr[::2] specifies a start of position 0 (the default), an end of the end of the list the default) and a step of 2. This means that if arr contains [1,2,3,4,5,6], then arr[::2] will be [1,3,5] (i.e. the values at all of the even indices) or if you specify a start position of 1 i.e. arr[1::2] you will get [2,4,6] (i.e. the values at all of the even indices).
So, rather using a while loop. You could use a for loop over just the even values:
total = 0
for even_val in arr[::2]:
total += even_val
print(total)
but for sum you can even more easily write is as a simple sum command on the list slice:
print(sum(arr[::2]))
Before Python 3.8, there was no simple equivalent of sum for product, so if you're using a lower version, you can either reuse the method above, allowing for the fact that you would need to prime the total with the first value, then multiply from the next one, i.e.:
total = arr[1]
count = 3
while count < n:
total *= arr[count]
count += 2
print(total)
or with a for loop:
total = arr[1]
for odd_val in arr[3::2]:
total *= odd_val
print(total)
But from Python 3.8 (documentation here) you can now import prod from the math library which will work in the same way as sum:
from math import prod
print(prod(arr[1::2]))
[Thanks #HeapOverflow for the nudge]
As this is for a problem set, it may not be an issue as all examples may have an array length N > 2, but the examples above do assume that there will be at least two entries in arr. If that's not the case, you should put in some validation before trying to access arr[1]
Here's a cute little recursive function to do that (assuming one-based indices):
# def prodSum(increment,multiplier=1,*rest): if zero based indices
def prodSum(multiplier,increment=0,*rest):
if not rest: return multiplier,increment
product,total = prodSum(*rest)
return (product * multiplier, total + increment)
x = [1,2,3,4,5]
print(prodSum(*reversed(x))) # 15,6

Problems in create function

That's my first question, I'm new at programming, sorry about any inconvinience !
I need to finish an exercise that consists in create a fuction to find the higher number and another function to find the lower number on a List, but when I print the results, it keeps giving me a wrong answer.
Here is my code:
lista_numeros = [1593, 200, 72, 8, 423, 2, 39, 293, 20120]
def menor_numero(arg1):
colector1 = arg1[0]
print(coletor1) # - Checking value added to COLECTOR1
for a in range(len(arg1)):
print(arg1[a]) # - Checking if the A argument is running through the list.
if colector1 < arg1[a]:
colector1 = arg1[a]
return colector1
resultado2 = menor_numero(lista_numeros)
print("menor ", str(resultado2)) # Result is the last position of the list, must be the 6th position.
Thank you very much.
Fist of all indentation is very important in python to tell it the order to execute your code and also to define where code sits within a loop etc.
Now you say you want to make a function that finds the smallest and largest number from the output of another function, for this I will assume this output is a list.
Please see code below with comments.
Mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9] #assume this is output from other funtion
def func(alist): #define function
collector1 = 100 #keeping your collector idea
for i in alist: #iterate through input
if i < collector1: #check if the item you are currently looking at is smaller than the item currently stored in collector
collector1 = i #if is smaller overwitre colletor with new item
print(collector1) #after iterating through all items in input print final value of colletor
func(Mylist) #call function with input
This outputs,
1
Simply change this,
if i > collector1:
And it will now find the largest in input, so output is now.
9
Edit: if you are looking for the smallest number start collector1 at a large number, if you are looking for the largest start collector1 at = 1.
assuming your input is a list and not a string or some sort you can just use the min()/max() methods:
myList = [1,2,3,4,5]
print(max(myList)) >> 5
print(min(myList)) >> 1
you can find more info here :
https://www.tutorialspoint.com/python3/list_max.htm
https://www.tutorialspoint.com/python3/list_min.htm
Your function is finding the maximum instead of the minimum value. Changing the < to > should do what you want.
Python also has builtin methods min() and max() which should do what you want.
#! python3
import random
numbers = []
max = 1000
min = 0
for i in range(40):
numbers.append(random.randint(min,max))
maxNum = min
minNum = max
for num in numbers:
if num > maxNum:
maxNum = num
elif num < minNum:
minNum = num
print(maxNum, minNum)
This is my code, I use the random library in python to generate a random list of numbers, then I set max to equal the maximum number in that list.
The following for loop generates 40 random numbers and appends them to my list.
I then set maxNum to equal zero, so everything will be greater than it, thereby the initial value will not influence the outcome, and then I set minNum to equal max, so every number will be less than it.
The last block of code loops through the numbers list, and compares every number with the current maxNum and minNum variables to see whether that number is greater than the max or less than the min. If it is, the maxNum (or minNum) number will be updated and the code will move on to the next number.
The last statement prints or minimum and maximum values.
I don't know what course you are taking, but I would advise you to familiarize yourself with this code and understand what it is doing, as it's pretty basic and the stuff you encounter in the future will be much harder.

how to make an imputed string to a list, change it to a palindrome(if it isn't already) and reverse it as a string back

A string is palindrome if it reads the same forward and backward. Given a string that contains only lower case English alphabets, you are required to create a new palindrome string from the given string following the rules gives below:
1. You can reduce (but not increase) any character in a string by one; for example you can reduce the character h to g but not from g to h
2. In order to achieve your goal, if you have to then you can reduce a character of a string repeatedly until it becomes the letter a; but once it becomes a, you cannot reduce it any further.
Each reduction operation is counted as one. So you need to count as well how many reductions you make. Write a Python program that reads a string from a user input (using raw_input statement), creates a palindrome string from the given string with the minimum possible number of operations and then prints the palindrome string created and the number of operations needed to create the new palindrome string.
I tried to convert the string to a list first, then modify the list so that should any string be given, if its not a palindrome, it automatically edits it to a palindrome and then prints the result.after modifying the list, convert it back to a string.
c=raw_input("enter a string ")
x=list(c)
y = ""
i = 0
j = len(x)-1
a = 0
while i < j:
if x[i] < x[j]:
a += ord(x[j]) - ord(x[i])
x[j] = x[i]
print x
else:
a += ord(x[i]) - ord(x[j])
x [i] = x[j]
print x
i = i + 1
j = (len(x)-1)-1
print "The number of operations is ",a print "The palindrome created is",( ''.join(x) )
Am i approaching it the right way or is there something I'm not adding up?
Since only reduction is allowed, it is clear that the number of reductions for each pair will be the difference between them. For example, consider the string 'abcd'.
Here the pairs to check are (a,d) and (b,c).
Now difference between 'a' and 'd' is 3, which is obtained by (ord('d')-ord('a')).
I am using absolute value to avoid checking which alphabet has higher ASCII value.
I hope this approach will help.
s=input()
l=len(s)
count=0
m=0
n=l-1
while m<n:
count+=abs(ord(s[m])-ord(s[n]))
m+=1
n-=1
print(count)
This is a common "homework" or competition question. The basic concept here is that you have to find a way to get to minimum values with as few reduction operations as possible. The trick here is to utilize string manipulation to keep that number low. For this particular problem, there are two very simple things to remember: 1) you have to split the string, and 2) you have to apply a bit of symmetry.
First, split the string in half. The following function should do it.
def split_string_to_halves(string):
half, rem = divmod(len(string), 2)
a, b, c = '', '', ''
a, b = string[:half], string[half:]
if rem > 0:
b, c = string[half + 1:], string[rem + 1]
return (a, b, c)
The above should recreate the string if you do a + c + b. Next is you have to convert a and b to lists and map the ord function on each half. Leave the remainder alone, if any.
def convert_to_ord_list(string):
return map(ord, list(string))
Since you just have to do a one-way operation (only reduction, no need for addition), you can assume that for each pair of elements in the two converted lists, the higher value less the lower value is the number of operations needed. Easier shown than said:
def convert_to_palindrome(string):
halfone, halftwo, rem = split_string_to_halves(string)
if halfone == halftwo[::-1]:
return halfone + halftwo + rem, 0
halftwo = halftwo[::-1]
zipped = zip(convert_to_ord_list(halfone), convert_to_ord_list(halftwo))
counter = sum([max(x) - min(x) for x in zipped])
floors = [min(x) for x in zipped]
res = "".join(map(chr, floors))
res += rem + res[::-1]
return res, counter
Finally, some tests:
target = 'ideal'
print convert_to_palindrome(target) # ('iaeai', 6)
target = 'euler'
print convert_to_palindrome(target) # ('eelee', 29)
target = 'ohmygodthisisinsane'
print convert_to_palindrome(target) # ('ehasgidihmhidigsahe', 84)
I'm not sure if this is optimized nor if I covered all bases. But I think this pretty much covers the general concept of the approach needed. Compared to your code, this is clearer and actually works (yours does not). Good luck and let us know how this works for you.

Categories