ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 20 May 2017 My Price 9.00

The function should return an array

Quiz 1:You need to design an iterative and a recursive function called replicate_iter and replicate_recur respectively which will receive two arguments: times which is the number of times to repeat and data which is the number or string to be repeated.

 

The function should return an array containing repetitions of the data argument. For instance, replicate_recur(3, 5) or replicate_iter(3,5) should return [5,5,5]. If the times argument is negative or zero, return an empty array. If the argument is invalid, raise a ValueError. And written in python.

 

this are the tests to consider;

import unittest

class ReplicateIterTestCases(unittest.TestCase):
  def test_it_returns_a_list_of_replicated_numbers(self):
    result = replicate_iter(3, 5)
    self.assertEqual(result, [5, 5, 5], msg='It should produce a list of three fives for the input 3, 5')

  def test_it_returns_an_empty_list_for_0_times(self):
    result = replicate_iter(0, 5)
    self.assertEqual(result,[], msg='It should return an empty list if times == 0')

  def test_it_returns_an_empty_list_for_negative_times(self):
    result = replicate_iter(-1, 20)
    self.assertEqual(result, [], msg='It should return an empty list for negative times.')

  def test_it_raises_value_error_if_invalid_input(self):
    with self.assertRaises(ValueError):
      replicate_iter(1, [])

  def test_it_raises_value_error_if_invalid_number_of_times_to_repeat(self):
    with self.assertRaises(ValueError):
      replicate_iter([], 3)

  def test_it_can_repeat_strings(self):
    result = replicate_iter(3, 'str')
    self.assertEqual(result, ['str', 'str', 'str'], msg='Should replicate strings too')
      
class ReplicateRecurTestCases(unittest.TestCase):
  def test_it_returns_a_list_of_replicated_numbers(self):
    result = replicate_recur(4, 5)
    self.assertEqual(result, [5, 5, 5, 5], msg='It should produce a list of four fives for the input 4, 5')

  def test_it_returns_an_empty_list_for_0_times(self):
    result = replicate_recur(0, 5)
    self.assertEqual(result,[], msg='It should return an empty list if times == 0')

  def test_it_returns_an_empty_list_for_negative_times(self):
    result = replicate_recur(-1, 20)
    self.assertEqual(result, [], msg='It should return an empty list for negative times.')
        
  def test_it_raises_value_error_if_invalid_input(self):
    with self.assertRaises(ValueError):
      replicate_recur(1, [])

  def test_it_raises_value_error_if_invalid_number_of_times_to_repeat(self):
    with self.assertRaises(ValueError):
      replicate_recur([], 3)


 

Quiz 2: 

Write a function called manipulate_data which will act as follows:

When given a list of integers, return a list, where the first element is the count of positives numbers and the second element is the sum of negative numbers.

 

tests to consider;

import unittest

class ManipulateDataTestCases(unittest.TestCase):
  def test_only_lists_allowed(self):
    result = manipulate_data({})
    self.assertEqual(result, 'Only lists allowed', msg='Invalid argument')

  def test_it_returns_correct_output_with_positives(self):
    result = manipulate_data([1, 2, 3, 4])
    self.assertEqual(result, [4, 0], msg='Invalid output')
    
  def test_returns_correct_ouptut_with_negatives(self):
    result = manipulate_data([1, -9, 2, 3, 4, -5]);
    self.assertEqual(result, [4, -14], msg='Invalid output')

Attachments:

Answers

(11)
Status NEW Posted 20 May 2017 10:05 AM My Price 9.00

-----------

Attachments

file 1495275486-Solutions file 2.docx preview (51 words )
H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly -----------onl-----------ine----------- an-----------d g-----------ive----------- yo-----------u e-----------xac-----------t f-----------ile----------- an-----------d t-----------he -----------sam-----------e f-----------ile----------- is----------- al-----------so -----------sen-----------t t-----------o y-----------our----------- em-----------ail----------- th-----------at -----------is -----------reg-----------ist-----------ere-----------d o-----------n -----------THI-----------S W-----------EBS-----------ITE-----------. ----------- Th-----------ank----------- yo-----------u -----------
Not Rated(0)