Question

In: Computer Science

Write a function that will accept a list of numbers and an integer (n). The function...

Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified.

For example, if the function is passed the list

[8, 3, 19, 26, 32, 12, 3, 7, 21, 16]

and the integer 3, it will return the list

[8, 26, 3, 16]

If the function is passed the list

[8, 3, 19, 26, 32, 12, 3, 7, 21, 16]

and the integer 4, it will return the list

[8, 32, 21]

The function should be general, and work for any list and any value of n.

Solutions

Expert Solution

The code in python is given below.

def nthElement(oldList,n):
    newList=[]
    newList.append(oldList[0])
    i=n
    while(i < len(oldList)):
        newList.append((oldList[i]))
        i+=n
    return newList

n1=int(input("Enter number of elements in the list: "))
inputList = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n1]
n=int(input("Enter the value of integer: "))
newList=nthElement(inputList,n)
print(newList)



The screenshot of the running code and output is given below.

If the answer helped please upvote it means a lot. For any query please comment.

Related Solutions

Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write a function that will accept one integer matrix E and one column vector F by...
Write a function that will accept one integer matrix E and one column vector F by reference parameters, and one integer I as a value parameter. The function will return an integer v, which is the i-th coefficient of row vector denoted by E*F (multiplication of E and F). For example, if V = E*F, the function will return v, which is equal to V[i]. Explain the time complexity of this function inside of the function code as a comment....
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
Write a function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
MIPS Assembly program: Accept N numbers from the user and sort the N numbers using any...
MIPS Assembly program: Accept N numbers from the user and sort the N numbers using any sorting algorithm. Print both the sorted array and unsorted array. N should be greater than or equal to 10.
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT