by CodeChum Admin
You can also make a list of numbers, ey? Let's go and have some fun in tweaking the values of each element, but let's give it some twist to add on some fun to the code.
Let's have a go at it!
Instructions:
Instructions
Output
The squares and cubes of the elements in the array.
4
1
1
8
27
.
.
.
# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [-10, -21, -4, -45, -66, 93, 11]
pos_count, neg_count = 0, 0
num = 0
# using while loop
while(num < len(list1)):
# checking condition
if list1[num] >= 0:
pos_count += 1
else:
neg_count += 1
# increment num
num += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list:
# Python program to count positive and negative numbers in a List
# list of numbers
list1 = [10, -21, 4, -45, 66, -93, 1]
pos_count, neg_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num >= 0:
pos_count += 1
else:
neg_count += 1
print("Positive numbers in the list: ", pos_count)
print("Negative numbers in the list:
# Python3 program to count minimum number of
# operations to get the given target array
# Returns count of minimum operations to
# convert a zero array to target array
# with increment and doubling operations.
# This function computes count by doing reverse
# steps, i.e., convert target to zero array.
def countMinOperations(target, n):
# Initialize result (Count of minimum moves)
result = 0;
# Keep looping while all elements of
# target don't become 0.
while (True):
# To store count of zeroes in
# current target array
zero_count = 0;
# To find first odd element
i = 0;
while (i < n):
# If odd number found
if ((target[i] & 1) > 0):
break;
# If 0, then increment
# zero_count
elif (target[i] == 0):
zero_count += 1;
i += 1;
# All numbers are 0
if (zero_count == n):
return result;
# All numbers are even
if (i == n):
# Divide the whole array by 2
# and increment result
for j in range(n):
target[j] = target[j] // 2;
result += 1;
# Make all odd numbers even by
# subtracting one and increment result.
for j in range(i, n):
if (target[j] & 1):
target[j] -= 1;
result += 1;
# Driver Code
arr = [16, 16, 16];
n = len(arr);
print("Minimum number of steps required to",
"\nget the given target array is",
countMinOperations(arr, n));
Comments
Leave a comment