Question
plz in Scheme

b. [2 marks] Create a function (alternate listi list2) that creates a list by alternating elements from the two given input l
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def alternate(list1,list2):

    i=0

    j=0

    new_list=[]

    while(i<len(list1) and j<len(list2)):

        new_list.append(list1[i])

        new_list.append(list2[j])

        i+=1

        j+=1

    while(i<len(list1)):

        new_list.append(list1[i])

        i+=1

    while(j<len(list2)):

        new_list.append(list2[j])

        j+=1

    return new_list

def count_x(x,new_list):  #x is a number to be counted

    return new_list.count(x)

def max_count(new_list):

    num_counts={}

    a=set(new_list)

    a=tuple(a)

    for number in new_list[::-1]:

        num_counts[number]=new_list.count(number)

    return max(num_counts)

def decreasing_instance(new_list):

    dec_nums=[]

    temp=[]

    flag='t'

    for i in  range(len(new_list)-1):

        if(new_list[i]>new_list[i+1]):

            if(flag=='t'):

                temp.append(new_list[i])

                flag='f'

            temp.append(new_list[i+1])

        elif(new_list[i]<new_list[i+1]):

            dec_nums.append(temp)

            flag='t'

            temp=[]

    for k in dec_nums:

        if len(k)==0:

            dec_nums.remove(k)

    dec_nums.remove([])

    return dec_nums

if __name__=='__main__':

    list1=list(map(int,input('Enter elements in list 1(Separated by space):').split()))

    list2=list(map(int,input('Enter elements in list 1(Separated by space):').split()))

    new_list=alternate(list1,list2)

    while(1):

        print('\n1.Print alternate list')

        print('\n2.Count any number')

        print('\n3.Maximum counts of any number')

        print('\n4.Decreasing instances from alternate list')

        print('\n5.Exit')

        choice=int(input('Enter your choice:'))

        if choice==1:

            print(new_list)

        elif choice==2:

            x=int(input('Enter any number to count:'))

            print(count_x(x,new_list))

        elif choice==3:

            print(max_count(new_list))

        elif choice==4:

            print(decreasing_instance(new_list))

        elif choice==5:

            import sys

            sys.exit()

#code written in python

Add a comment
Know the answer?
Add Answer to:
plz in Scheme b. [2 marks] Create a function (alternate listi list2) that creates a list...
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
  • Language in Scheme e. (5 marks] Create a procedure (decreasing L) that returns a list of...

    Language in Scheme e. (5 marks] Create a procedure (decreasing L) that returns a list of all of the consecutive decreasing subseqeuences in the input list. E.g., (decreasing (3 6 8 9 7 4 8 6 3)) + ((974) ( (decreasing '(7 6 5 4 8 5 2 5 1 5 2 1)) + (( 7 (decreasing '(1 2 3 4 5)) () 4) (8 6 3) (63)) 5 4)(654) (54)(8 5 2)(5 2) (5 1) (5 2 1) (2...

  • I need help with this problem this is what I put and these are the errors...

    I need help with this problem this is what I put and these are the errors I got Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of listi followed by the last element of list2, followed by the second to last element of listi, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements...

  • JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given...

    JAVA - Write a function in your Main class LinkedList<E> mergeLists(LinkedList<E> list1, LinkedList<E> list2) - given two ordered linked lists of integers, merge the two lists into a single LinkedList whose elements are in sorted order. You should create a new LinkedList and add the values from list1 and list2 into the new list in sorted order. Do not modify list1 or list 2. list1 => 1 -> 3 -> 7 -> 8, list2 => 2 -> 5 -> 7...

  • How to remove all occurrences of 2 from list2. Then display the list. Python import random...

    How to remove all occurrences of 2 from list2. Then display the list. Python import random list1 = [] for i in range (10): list1.append(random.randint(1,4)) print('List 1:') print(list1) list2 = [] list2.append(list1[5:]) print('List 2:') print(list2) list2.remove ******** (This is the part I am having trouble on!!) print('List 2 with all 2s removed:') print(list2) Sample output: List 1: [4, 1, 1, 3, 3, 3, 2, 4, 2, 4] List2: [3, 2, 4, 2, 4] List 2 with all 2s removed: [3,...

  • Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as o...

    Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as output List2. List2 contains all items that appear in List1, except that repeated or duplicate items are not present in List1. For example: ?- remdup([1,2,3] ,x) ?- remdup([3,1,2,31,x) X=[1, 2, 3]. Note that the order of items in List2 is unspecified: for instance, [3,1,2 is an acceptable result for the second example above. 4. 2 marks Consider a course...

  • A) [90 marks] Write a program that .Splits a list into two halves. The function split must modify...

    IN C LANGUEGA(PROGRAMİN C) a) [90 marks] Write a program that .Splits a list into two halves. The function split must modify the original list and return a pointer to the second half of the list, e.g., L = 1 → 2 → 3 → 4 must be 1 2 after execution, and the function must return 3 4 If the size of the original list is odd, add a node with value 99 to the index 4 (recall that...

  • 7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest va...

    7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest value to the first position on the first pass and so on. The pseudocode for this version is shown below. The procedure swap is used to interchange the values in its two arguments. In the box, rewrite the contents of the list 5, 2, 3, 4, 1 every time it changes, in this5, 2, 3, 4, 1 new version of bubblesort. Walk through each...

  • 2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new...

    2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new list that contains only the even numbers. (Hint: use reminder function) Sample runs: (get-even-nums ‘(1 2 3 4 5 6 7 8 9)) should return (8 6 4 2). (get-even-nums ‘(1 3 5 7 9)) should return ’( ).

  • 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...

  • Create a Scheme function for the following: Thank you! 2. combine which has 3 arguments that...

    Create a Scheme function for the following: Thank you! 2. combine which has 3 arguments that are all lists. The result should be equivalent to the concatenation of the first list, the reverse of the second list, followed by the third list. Use the built-in functions append and reverse. For example, (combine '(1 2) (3 4 5) '(6 7)) should return the list '(1 2 5 4 3 6 7).

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