Question

Need help with this question. Write a function take first n that takes a list l...

Need help with this question.

Write a function take first n that takes a list l and an integer n as arguments. The function should return (as a new list) the first n values from the list. In the event that n is greater than the length of l , the entire list should be returned.

I know this is correct thus far

def take_first_n(l, n):
return

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The function being written C++

#include<iostream>
using namespace std;
int list[10]; //returning list is taken as global so as to access from main()

int* take_first_n(int *l, int n) //list pointer l, and number n as parameters
{
int i,count=0; //loop variable i and counter for counter number in the list count
for(int j=0;l[j]!='';j++) //number of elements in the list being counted
   count++;
if(count<n) // checking if list is smaller than n
return l; //if list is small original list is returned
for(i=0; i<n; i++) //if n is smaller first n elements taken in new list
list[i]=l[i];
return (list); //new list is returned
}

//above code can be tested using the following sample main with hard coded list a having 10 elements

int main()
{
   int a[20]={1,2,3,4,5,6,7,8,9,11},*p,n; //*p is the list pointer
   cout<<"enter number n: "; // value of n from console
   cin>>n;
   p=take_first_n(a,n); //function call
   n=0; //n set to 0 after function call to print the list after counting no. of digits
   for(int j=0;p[j]!='';j++) // counting numbers in new list
   n++;
   for(int i=0;i<n;i++) //printing returned list
   cout<<p[i]<<' ';
   return 0;
}

The output for the two cases are:

1. when value of n less than list size:

2. when value of n is greater than number of elements in the list:

Add a comment
Know the answer?
Add Answer to:
Need help with this question. Write a function take first n that takes a list l...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • PYTHON The function longest that returns the longest of two strings. The function count_over that takes...

    PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...

  • Python 2.7 Write a function cumsum() that takes a list l as argument and returns the...

    Python 2.7 Write a function cumsum() that takes a list l as argument and returns the cumulative sum (also known as the prefix sum) of l, which is a list, say cs of the same length as l such that each element cs[i] is equal to the sum of the first i + 1 elements of l, i.e., cs[i] == l[0] + l[1] + l[2] + ... + l[i] You should not modify the argument list l in any way....

  • Please I need help with this. Thank you Write the function cycle of type 'a list...

    Please I need help with this. Thank you Write the function cycle of type 'a list *int-> 'a list that takes a list and an integer n as input and returns the same list, but with the first element cycled to the end of the list n times. For example, cycle ([1,2,3,4,5,6], 2) should return [3,4,5,6,1,2)]. You can make use of the cycle 1 function given below as a helper function. fun cycle1 list- tl list hd list]:

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount,...

    Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount, an integer specifying the number of “buckets” to return and numberLs, a list of numbers ) buckets should split the range of values in numberLs into bucketCount sub-ranges. Specifically buckets should return a list-of-lists of length bucketCount. Each list in the returned list-of-lists represents a “bucket”. Each bucket should contain the numbers from numberLs whose values are greater than or equal to min(numberLs) +...

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • 1. Question: Write a function that takes a list L and returns a random sublist of...

    1. Question: Write a function that takes a list L and returns a random sublist of size N of that list. Assume that the indexes must be in increasing order. That is, you cannot go backwards Example L [1, 2, 3, 4, 5] 5 The function should return one of these lists: [2, 3, 4] [2, 3, 5] [2, 4, 5] [3, 4, 5]

  • Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e....

    Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e. no duplicated integers), and should return: 1. List (of tuples) containing all pairs of numbers in L that are coprime. 2. Empty list, If no pair of numbers in L is coprime. Your solution must include a function call to the function coprime designed in the previous question. The order of the tuples or the order of the two numbers within the tuples is...

  • IN PYTHON Write a function called "def top_prices(prices, n):", which will take two arguments. The first...

    IN PYTHON Write a function called "def top_prices(prices, n):", which will take two arguments. The first argument is the list of prices, each price should be a number type (int, float). The second argument 'n' indicates top number of prices. For example, if n-3, this function should return a new list of the top 3 prices, sorted from lowest to highest. If n-10, this function should return a new list of the top 10 prices, sorted from lowest to highest....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT