Problems Flashcards

1
Q

Write a program that takes a user input of a number and prints out the numbers from 1 to that number. For example, if the user enters the number 5, the program should print out 1, 2, 3, 4, and 5.

A

num = int(input(“Enter a number: “))

for i in range(1, num+1):
print(I)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a program that takes a user input of a string and prints out the string in reverse. For example, if the user enters the string “Hello World”, the program should print out “dlroW olleH”.

A

sentence = input(“Enter a sentence: “)

for i in range(len(sentence)-1, -1, -1):
print(sentence[I], end=””)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

read the data within a .txt file

A

with open(“file.txt”, “r”) as c:
myCals = [line.strip() for line in c]

“r” = read mode
line.strip() = new list element for every line
c = variable name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to add integers subgroups together within a list

A

caloriesums = []
caloriesum = 0

for i in myCals:
if i == ‘’:
caloriesums.append(caloriesum)
caloriesum = 0
else:
caloriesum = caloriesum + int(i)

answer = sum(caloriesums[:3])

answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to sort integers within a list to make the highest numbers come first

A

caloriesums.sort(reverse=True)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to get the first three elements of a list and add them together

A

answer = sum(caloriesums[:3])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you want to add values to a list from a text file whilst using a function to calculate values withing the list

A

for i in rpsRounds:
roundValue.append(scoreTally(i[0], i[2]))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given the array nums consisting of 2n elements in the form [x1,x2,…,xn,y1,y2,…,yn].

Return the array in the form [x1,y1,x2,y2,…,xn,yn].

A

Build an array result of size 2 * n.
Iterate over the nums array ranging from indices 0 to n - 1:
Store the element xi+1x_{i + 1}x
i+1

, that is, nums[i] at index 2 * i,
and element yi+1y_{i + 1}y
i+1

, that is, nums[i + n] at index 2 * i + 1 in result.
Return the result array.

class Solution:
def shuffle(self, nums: List[int], n: int) -> List[int]:
result = [0] * (2 * n)
for i in range(n):
result[2 * i] = nums[i]
result[2 * i + 1] = nums[n + i]
return result

the more complicated way requires bit manipulation. here is the link to the solution: https://leetcode.com/problems/shuffle-the-array/solutions/2973933/shuffle-the-array/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is n−1

A

In the context of arrays, n-1 can also refer to the index of the last element in the array. This is because arrays in programming languages typically use zero-based indexing, meaning that the first element of the array has index 0, the second element has index 1, and so on.

For example, if you have an array “arr” with length n, then the index of the last element in the array would be n-1, because the indices of the elements range from 0 to n-1.

So, if n is the length of the array, then arr[n-1] would refer to the last element of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Build an array that is twice the size of n.

n = 6

A

array = [0](2n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

for i in range(n):
result[2 * i] = nums[i]
result[2 * i + 1] = nums[n + i]

What is interesting about this

A

The second line of code will add numbers to an array at positions

0,2,4….

the third line of code will add numbers to an array at positions

1,3,5 etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can we assume the input is valid?

What does this mean?

A

In the context of coding, the question “Can we assume the input is valid?” refers to whether or not the code can assume that the data that is being inputted into the system meets the criteria or constraints that have been specified for it. If the input is assumed to be valid, the code may not check for or handle cases where the input is invalid, potentially leading to unexpected behavior or errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly