Question
Help with solution using Python

Riffle def riffle(items, out True): Given a list of items that is guaranteed to contain an even number of elements (note that

test using

def riffle_generator(seed):
random.seed(seed)
for i in range(1000):
n = random.randint(0, 100)
items = [random.randint(0, 10**6) for j in range(2 * n)]
yield (items, True)
yield (items, False)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

import random

def genrate(items, isOut):
   middleIndex=len(items)/2
   riffledItems=[0]*len(items);
   j=0;
   for i in range(middleIndex): #indexes from 0 to lenght/2
       if(isOut):
           riffledItems[j]=items[i] #maintain a indexing paramenter j and assign values accordigly
           j=j+1
           riffledItems[j]=items[i+middleIndex] #for out riffling add len/2 value to i value for getting
           j=j+1
       else:
           riffledItems[j]=items[i+middleIndex] #if it is false, do the assignment in reverse order.
           j=j+1
           riffledItems[j]=items[i]
           j=j+1
   return riffledItems

def riffle_generator(seed):
   items=[]
   random.seed(seed)
   for i in range(1000):
       n = random.randint(0, 100)
       items = [random.randint(0, 10**6) for j in range(2 * n)]
   #items=[1,2,3,4,5,6] Un comment this for testing purpose
   print "out riffle",genrate (items, True)
   print "in riffle",genrate(items, False)

riffle_generator(2)

code refrence image:

fe py import random Eder genrate (items, isout): 3 middle Index -len (items)/2 riffledItems-[0]*len (items); i-0; for i in ra

output:

GC:Windows System32\cmd.exe D: \SEGG>python riffle.py out riffle [440085, 373745, 689175, 415013, 37441, 424687, 652176, 1914output with sample array:

D:\SEGG>python riffle.py out riffle [1, 4, 2, 5, 3, 6] in riffle [4, 1, 5, 2, 6, 3] D:\SEGG>

Add a comment
Know the answer?
Add Answer to:
Help with solution using Python test using def riffle_generator(seed): random.seed(seed) for i in range(1000): n =...
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
  • How to solve this Python problem? Calling all units, B-and-E in progress def is..kerfectbeker(n): A positive...

    How to solve this Python problem? Calling all units, B-and-E in progress def is..kerfectbeker(n): A positive integer n is said to be a perfect power if it can be expressed as the power b**e for some two integers band e that are both greater than one. (Any positive integer n can always be expressed as the trivial power n**1, so we don't care about that.) For example, the integers 32, 125 and 441 are perfect powers since they equal 2**5,5**3...

  • def selectionSortK(alist, k): for i in range(0,len(alist) - 1): min = i for j in range(i + 1, len(alist)): if alist[j] < alist[min]: min = j temp = alist[i] alist[i] = alist[min] alist[min] = temp...

    def selectionSortK(alist, k): for i in range(0,len(alist) - 1): min = i for j in range(i + 1, len(alist)): if alist[j] < alist[min]: min = j temp = alist[i] alist[i] = alist[min] alist[min] = temp P3: Sanity Test: Is selectionSortK callable? ... ok test_doNothing (__main__.TestProblem3) P3: Does sorting the first k elements with k=0 do nothing? ... ok test_onePass (__main__.TestProblem3) P3: Sorting a portion of a decreasing list ... FAIL test_severalPasses (__main__.TestProblem3) P3: Sorting a decreasing list in several stages...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

  • *URGENT JAVA PROGRAMMING LAB* so I need to write some classes for my lab if someone...

    *URGENT JAVA PROGRAMMING LAB* so I need to write some classes for my lab if someone could give me an outline or just how I should so it that would be awesom here is the questions please help ASAP A. A histogram is used to plot tabulated frequencies. Create a Histogram class that can be used to maintain and plot the frequencies of numbers that fall within a specified range. The Histogram class contain will an array of counters with...

  • Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in...

    Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in PYTHON, new student. Please, please, I'm begging, Kindly answers all the questions. I'm hoping to grant my request. Thanks in advanced. 1.) What is the output of the following snippet? l1 = [1,2] for v in range(2): l1.insert(-1,l1[v]) print(l1)        a.) [1, 2, 2, 2]        b.) [1, 1, 1, 2]        c.) [1, 2, 1, 2]        d.) [1,...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...

    PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

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