The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
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')
-----------