Question

In: Computer Science

Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py. Write...

Name your functions countOnesLoop() and countOnesWhere() and submit your code in a file named countOnes.py.

Write a function that consists of a set of loops that run through an array and count the number of ones in it. Do the same thing using the where() function (use info(where) to find out how to use it) (in python)

Solutions

Expert Solution

Source Code:

import numpy as np          #it is used to implement array
def countOnesLoop(arr):
    count=0;
    for i in arr:
        if i==1:
            count+=1
    return count

def countOnesWhere(arr):
    for i in range(len(arr)):
        if arr[i]==1:
            print('1 found in index ',i)

def main():
    arr =np.array([1, 2, 3, 4, 1, 5, 6, 1, 7, 1])
    count=countOnesLoop(arr)
    print("Total no of 1 is ",count)
    countOnesWhere(arr)
    where=np.where(arr==1)          #where() method
    print('1 found in index ',where)

if __name__=="__main__":
    main()

Output:


Related Solutions

Name your program file warmup.py Submit your working Python code to your CodePost.io account. In this...
Name your program file warmup.py Submit your working Python code to your CodePost.io account. In this challenge, establish if a given integer num is a Curzon number. If 1 plus 2 elevated to num is exactly divisible by 1 plus 2 multiplied by num, then num is a Curzon number. Given a non-negative integer num, implement a function that returns True if num is a Curzon number, or False otherwise. Examples is_curzon(5) ➞ True 2 ** 5 + 1 =...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double...
Submit a c++ file: 2. Write a program that defines the named constant PI, const double PI = 3.14159;, which stores the value of p. The program should use PI and the functions listed in Table 6-1 to accomplish the following: a. Output the value of Pi Output the value of Pi. b. Prompt the user to input the value of a double variable r, which stores the radius of a sphere. The program then outputs the following: i.   The...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the user for a command, run it, then prompt the user for their next command. If the user types "exit", then the shell should terminate. The shell should ignore Ctrl-C. Inside the attached parse.h header file, there is a parse function that you can use to split up the command line into separate strings. Recall that execvp accepts two arguments: the name of the command,...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible...
Create a file named work.sh in your hw5 directory. Give this file read and write permission...
Create a file named work.sh in your hw5 directory. Give this file read and write permission (no execute permissions) for you alone. No other account should have any access privileges to this file. Change the permissions on the work.sh file so you have read and write permissions. Give everybody else, including the group, read permissions only. Give yourself read, write and execute permissions to the file work.sh. Give everyone else, including the group, execute permissions only. Create a directory named...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
Create one Excel file to submit named "Exam 3 Team X.xlsx" where X is your team...
Create one Excel file to submit named "Exam 3 Team X.xlsx" where X is your team letter. Show work for credit. USE EXCEL 1. Use the data below. Team C Date DOW 10YTREAS 8-Apr-20 23,719.37 0.729 1-Apr-20 22,653.86 0.736 25-Mar-20 21,917.16 0.698 18-Mar-20 20,704.91 0.816 11-Mar-20 21,237.38 0.997 4-Mar-20 25,018.16 0.748 26-Feb-20 25,917.41 1.01 19-Feb-20 27,081.36 1.33 12-Feb-20 29,232.19 1.556 5-Feb-20 29,276.34 1.59 29-Jan-20 28,807.63 1.603 22-Jan-20 28,722.85 1.641 15-Jan-20 29,196.04 1.769 8-Jan-20 28,939.67 1.818 1-Jan-20 28,583.68 1.827 a. Create...
Write all your answers for this problem in a text file named aa.txt – for each...
Write all your answers for this problem in a text file named aa.txt – for each problem write the problem number and your answer. 1.1 What type of object can be contained in a list (write the letter for the best answer)? a. String b. Integer c. List d. String and Integer only e. String, Integer, and List can all be contained in a list 1.2 Which statement about the debugger is not correct? a. It is a powerful tool...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT