# Code 1: sum of first 20 natural numbers (+1 and -1 as increments)
total = 0
for i in range(1, 21):
total += 1 # adding +1 twenty times
print("Sum of twenty +1s:", total) # Should print 20
# Now subtract 1 twenty times
for i in range(20):
total -= 1 # subtracting 1 twenty times
print("Sum after subtracting twenty 1s:", total) # Should print 0
python
# Code 2: Generate a 20-element list of alternating +1 and -1 elements
arr = []
for i in range(20):
if i % 2 == 0:
arr.append(1)
else:
arr.append(-1)
print(arr)
python
# Code 3: Count how many +1 and -1 in a random sequence of 20 elements
import random
sequence = [random.choice([1, -1]) for _ in range(20)]
count_pos = 0
count_neg = 0
for num in sequence:
if num == 1:
count_pos += 1
else:
count_neg += 1
print("Sequence:", sequence)
print("+1 count:", count_pos)
print("-1 count:", count_neg)
python
# Code 4: Create a 20-line code block computing cumulative sums of alternating +1/-1
cumulative = 0
cumulatives = []
for i in range(20):
if i % 2 == 0:
cumulative += 1
else:
cumulative -= 1
cumulatives.append(cumulative)
print("Cumulative sum list:", cumulatives)
python
# Code 5: Flip signs in a list of +1 and -1 twenty times
values = [1, -1] * 10
for i in range(20):
values = [1 if x == -1 else -1 for x in values]
print("Values after flipping signs 20 times:", values)
python
# Code 6: Calculate difference between counts of +1 and -1 in a list
vals = [1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, 1]
count_pos = vals.count(1)
count_neg = vals.count(-1)
diff = count_pos - count_neg
print("Difference between +1 and -1 counts:", diff)
python
# Code 7: Print +1 or -1 twenty times with a condition: if line number divisible by 3, print -1 else +1
for i in range(1, 21):
if i % 3 == 0:
print(-1)
else:
print(1)
python
# Code 8: Generate a string of 20 characters consisting of '+' for +1 and '-' for -1 corresponding to a list.
lst = [1 if i % 2 == 0 else -1 for i in range(20)]
result = ""
for val in lst:
if val == 1:
result += "+"
else:
result += "-"
print(result)
python
# Code 9: Calculate product of a sequence of alternating +1 and -1 values (20 elements)
product = 1
for i in range(20):
if i % 2 == 0:
product *= 1
else:
product *= -1
print("Product of alternating 20 elements:", product)
python
# Code 10: Build a dictionary counting how many times +1 and -1 appear in a generated list of length 20
from collections import defaultdict
lst = [1 if i % 3 == 0 else -1 for i in range(20)]
counts = defaultdict(int)
for x in lst:
counts[x] += 1
print("Counts dictionary:", dict(counts))
python
# Code 1: Generate a 20-element list of +1 or -1 using random choice and sum them up
import random
values = []
total = 0
for _ in range(20):
val = random.choice([1, -1])
values.append(val)
total += val
print("Values:", values)
print("Sum of values:", total)
python
# Code 2: Create a 20-line code that flips the sign of a number starting from 1 twenty times
num = 1
history = []
for _ in range(20):
num = -num # flip sign
history.append(num)
print("Flipped signs over 20 iterations:", history)
python
# Code 3: Construct a cumulative sum array from alternating +1 and -1 for 20 steps
result = []
cumulative = 0
for i in range(20):
if i % 2 == 0:
cumulative += 1
else:
cumulative -= 1
result.append(cumulative)
print("Cumulative sums:", result)
python
# Code 4: Check the parity of indices and assign +1 for even and -1 for odd indices, then count occurrences
sequence = []
count_pos = 0
count_neg = 0
for i in range(20):
if i % 2 == 0:
sequence.append(1)
count_pos += 1
else:
sequence.append(-1)
count_neg += 1
print("Sequence:", sequence)
print("+1 count:", count_pos)
print("-1 count:", count_neg)
python
# Code 5: Given a list of twenty +1 or -1 values, calculate and print the running total after each addition
lst = [1 if i % 3 != 0 else -1 for i in range(20)]
running_total = 0
for index, val in enumerate(lst):
running_total += val
print(f"After element {index + 1} (value={val}), running total is {running_total}")
python
# Code 6: Generate a string of '+' and '-' characters representing +1 and -1 over twenty indices
symbols = []
for i in range(20):
if i % 4 in (0,1):
symbols.append('+')
else:
symbols.append('-')
output_string = ''.join(symbols)
print("Generated symbol string:", output_string)
python
# Code 7: Modify a list of twenty elements initialized as +1 to -1 at every 5th index, then count
lst = [1] * 20
for i in range(0, 20, 5):
lst[i] = -1
count_plus = lst.count(1)
count_minus = lst.count(-1)
print("List:", lst)
print(f"Count of +1: {count_plus}, Count of -1: {count_minus}")
python
# Code 8: Iterate from 1 to 20 and print +1 or -1 depending on whether the number is prime or not
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
for num in range(1, 21):
if is_prime(num):
print(1)
else:
print(-1)
python
# Code 9: Generate a list of length 20 where each element is +1 if the preceding element was -1 and vice versa, starting from +1
lst = [1]
for _ in range(19):
lst.append(-lst[-1])
print("Alternating list:", lst)
python
# Code 10: Calculate the final sum starting from zero and adding +1 or -1 twenty times, randomly chosen
import random
total = 0
for _ in range(20):
total += random.choice([1, -1])
print("Final sum after 20 random +1/-1 additions:", total)
python
# Code 1: Generate a sequence of 20 +1 and -1 based on index parity, print running sum each step
running_sum = 0
sequence = []
for i in range(20):
if i % 2 == 0:
val = 1
else:
val = -1
sequence.append(val)
running_sum += val
print(f"Index {i}: Value = {val}, Running Sum = {running_sum}")
print("Final sequence:", sequence)
print("Final running sum:", running_sum)
python
# Code 2: Create a 20-line program that toggles a value between +1 and -1 and stores each step
current = 1
values = []
for i in range(20):
values.append(current)
# toggle value
current = current * -1
print("Toggled values over 20 iterations:", values)
python
# Code 3: Generate two lists of +1 and -1 for 20 iterations each, then compute element-wise sum and print results
list1 = []
list2 = []
for i in range(20):
list1.append(1 if i % 3 == 0 else -1)
list2.append(-1 if i % 2 == 0 else 1)
sum_list = []
for i in range(20):
sum_list.append(list1[i] + list2[i])
print(f"Index {i}: list1={list1[i]}, list2={list2[i]}, sum={sum_list[i]}")
print("Final sums:", sum_list)
python
# Code 4: Build a cumulative sum list by alternating adding and subtracting 1 for 20 steps
cumulative = 0
results = []
for step in range(20):
if step % 2 == 0:
cumulative += 1
else:
cumulative -= 1
results.append(cumulative)
print(f"Step {step+1}: Cumulative sum = {cumulative}")
print("Cumulative sums after 20 steps:", results)
python
# Code 5: Initialize a list with twenty 1's; then replace every second element with -1; print counts
vals = [1] * 20
for idx in range(1, 20, 2):
vals[idx] = -1
count_pos = vals.count(1)
count_neg = vals.count(-1)
print("List:", vals)
print(f"Count of +1: {count_pos}")
print(f"Count of -1: {count_neg}")
python
# Code 6: Generate and print a 20-line sequence where each line prints +1 if sum of all previous lines was even else -1
total_sum = 0
sequence = []
for i in range(20):
if total_sum % 2 == 0:
val = 1
else:
val = -1
sequence.append(val)
total_sum += val
print(f"Line {i+1}: {val}, Current Sum: {total_sum}")
print("Final sequence:", sequence)
python
# Code 7: Build a list of length 20 with +1 in prime indices and -1 otherwise; includes prime checking function
def is_prime(n):
if n <= 1:
return False
for x in range(2, int(n ** 0.5) + 1):
if n % x == 0:
return False
return True
sequence = []
for i in range(20):
if is_prime(i):
sequence.append(1)
else:
sequence.append(-1)
print("Sequence based on prime indices:", sequence)
python
# Code 8: Perform twenty iterations accumulating +1 and -1 where +1 is added if index mod 5 < 3 else -1, printing intermediate sums
total = 0
for i in range(20):
if i % 5 < 3:
total += 1
else:
total -= 1
print(f"Iteration {i+1}: Total = {total}")
print("Final total after 20 iterations:", total)
python
# Code 9: Create a list of 20 elements alternating +1 and -1 starting from -1, and print cumulative sum after each addition
values = []
cumulative_sum = 0
current = -1
for i in range(20):
values.append(current)
cumulative_sum += current
print(f"Step {i+1}: Added {current}, Cumulative Sum = {cumulative_sum}")
current = -current # toggle sign
print("Final values array:", values)
python
# Code 10: Count how often +1 or -1 appear in a generated list of 20 random +1/-1 and print the frequency
import random
lst = [random.choice([1, -1]) for _ in range(20)]
count_pos = 0
count_neg = 0
for val in lst:
if val == 1:
count_pos += 1
else:
count_neg += 1
print("Generated list:", lst)
print(f"Frequency of +1: {count_pos}")
print(f"Frequency of -1: {count_neg}")
python
# Code 1: Generate 20 values alternating +1 and -1, then calculate running total and print each step
running_total = 0
values = []
for i in range(20):
if i % 2 == 0:
val = 1
else:
val = -1
values.append(val)
running_total += val
print(f"Step {i+1}: Value={val}, Running total={running_total}")
print("Final values list:", values)
print("Final running total:", running_total)
python
# Code 2: Using a list of 20 random +1/-1, compute cumulative sums and print with descriptive output
import random
lst = [random.choice([1, -1]) for _ in range(20)]
cumulative_sum = 0
cumulative_sums = []
for idx, val in enumerate(lst):
cumulative_sum += val
cumulative_sums.append(cumulative_sum)
print(f"Index {idx}: Value={val}, Cumulative sum={cumulative_sum}")
print("Original list:", lst)
print("List of cumulative sums:", cumulative_sums)
python
# Code 3: Create a sequence of twenty values where every third element is -1 and the rest +1, then count occurrences
sequence = []
count_pos = 0
count_neg = 0
for i in range(20):
if (i + 1) % 3 == 0:
val = -1
count_neg += 1
else:
val = 1
count_pos += 1
sequence.append(val)
print(f"Index {i}: Value={val}")
print("Sequence:", sequence)
print(f"+1 count: {count_pos}, -1 count: {count_neg}")
python
# Code 4: Initialize a value with 1, toggle sign 20 times, printing results and storing the history
value = 1
history = []
for i in range(20):
history.append(value)
print(f"Iteration {i + 1}: Value = {value}")
value = -value
print("History of values:", history)
python
# Code 5: Generate a repeating pattern of [+1, +1, -1, -1] for 20 elements and print the partial sums at each step
pattern = [1, 1, -1, -1]
values = []
for i in range(20):
values.append(pattern[i % 4])
partial_sum = 0
for i, val in enumerate(values):
partial_sum += val
print(f"Index {i}: Value = {val}, Partial sum = {partial_sum}")
print("Final list:", values)
python
# Code 6: Produce a list with 20 elements where a +1 is added for even primes indices and -1 otherwise; print result
def is_prime(num):
if num < 2:
return False
for j in range(2, int(num ** 0.5) + 1):
if num % j == 0:
return False
return True
lst = []
for i in range(20):
if is_prime(i) and i % 2 == 0:
val = 1
else:
val = -1
lst.append(val)
print("Generated list:", lst)
python
# Code 7: Compute sum of twenty values where values alternate between +1 and -1 starting with -1, print each step
current_value = -1
total = 0
for i in range(20):
total += current_value
print(f"Step {i+1}: Added {current_value}, Total = {total}")
current_value = -current_value
print("Final total:", total)
python
# Code 8: Create a list of 20 elements where elements are +1 if index mod 4 < 2 else -1, count values, print output
lst = []
count_plus = 0
count_minus = 0
for i in range(20):
if (i % 4) < 2:
lst.append(1)
count_plus += 1
else:
lst.append(-1)
count_minus += 1
print(f"Index {i}: Value = {lst[-1]}")
print("Final list:", lst)
print(f"Number of +1's: {count_plus}, Number of -1's: {count_minus}")
python
# Code 9: Generate a pattern of twenty +1 and -1 based on if index is divisible by 5 and print cumulative sum each step
cumulative = 0
for i in range(20):
val = 1 if (i % 5 != 0) else -1
cumulative += val
print(f"Iteration {i + 1}: Value = {val}, Cumulative sum = {cumulative}")
print(f"Final cumulative sum after 20 iterations: {cumulative}")
python
# Code 10: Generate a list of +1 and -1 of length 20, alternating signs, but flip sign every 7 steps, print values
lst = []
sign = 1
for i in range(20):
lst.append(sign)
print(f"Index {i}: Value = {sign}")
if (i + 1) % 7 == 0:
sign = -sign # flip sign after every 7 elements
else:
sign = sign * -1
print("Final generated list:", lst)