Answer to Question #254369 in Python for Hazel

Question #254369

2. Cubes and Squares

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:

  1. An array containing 40 integer elements is already provided for you in the code editor below.
  2. Using loops and conditions, print out the cube of the array element if it is a positive number, and print out the square of the number if it is negative. Each result must be printed out separately by each line.

Instructions

  1. A list containing 5 integer elements is already provided for you in the code editor below.
  2. Using loops and conditions, print out the cube of the list element if it is a positive number, and print out the square of the number if it is negative. Each result must be printed out separately by each line.

Output

The squares and cubes of the elements in the array.

4
1
1
8
27
.
.
.
1
Expert's answer
2021-10-21T16:03:11-0400
# 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)); 

 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS