Sorry For Inconvenience, Site is Under Maintenance. Please Goto HomePage

Python Qualis Fresco Play Handson Solution

Python Qualis Fresco Play Handson Solution
Notes Bureau

Python Qualis Fresco Play Handson Solution

Disclaimer: The main motive to provide this solution is to help and support those who are unable to do these courses due to facing some issue and having a little bit lack of knowledge. All of the material and information contained on this website is for knowledge and education purposes only.


Try to understand these solutions and solve your Hands-On problems. (Not encourage copy and paste these solutions)

Python Qualis Fresco Play Handson Solution

Course Path: Data Science/DATA SCIENCE BASICS/Python Qualis

Suggestion: Just Copy whole code from below and replace with existing code on hackerrank.


Please follow the below steps before Run Tests to run your code successfully otherwise you will face some server issues even Hands-On code is correct.

Step 1: Run->Install
Step 2: Run->Run
Step 3: Run Tests


1.Doctest


Doctest 1


  

#!/bin/python3

import math

import os

import random

import re

import sys

import inspect

# Complete the following isPalindrome function:

def isPalindrome(x):

    # Write the doctests:

    """

    >>>isPalindrome(121)

    True

    >>>isPalindrome(344)

    False

    >>>isPalindrome(-121)

    Traceback (most recent call last):

    ValueError: x must be a positive integer

    >>>isPalindrome("hello")

    Traceback (most recent call last):

    TypeError: x must be an integer

    """

    # Write the functionality:

    y = list(str(x))[::-1]

    y = "".join([i for i in y])

    try:

        if type(x) == type(1):

            try:

                if x>0:

                    if str(x) == y:

                        return True

                    else:

                        return False

                else:

                    raise ValueError('x must be a positive integer')

            except ValueError as e:

                print(e)

        else:

            raise TypeError('x must be an integer')

    except TypeError as r:

        print(r)


if __name__ == '__main__':

Doctest 2

import inspect
import doctest
import re
import math
# Define the class 'Circle' and its methods with proper doctests:
class Circle:
    def __init__(self, radius):
        # Define doctests for __init__ method:
        """
        >>> c1 = Circle(2.5)
        >>> c1.radius
        2.5
        """
        self.radius = radius
    def area(self):
        # Define doctests for area method:
        """
        >>> c1 =Circle(2.5)
        >>> c1.area()
        19.63
        """
        return round(math.pi*(self.radius**2),2)
        # Define area functionality:
    def circumference(self):
        # Define doctests for circumference method:
        """
        >>> c1=Circle(2.5)
        >>> c1.circumference()
        15.71
        """
        return round(math.pi* (self.radius*2),2)
        # Define circumference functionality:
if __name__ == '__main__':

2.Welcome to Python Qualis - Nose Testing Framework

from proj.circle import Circle
from nose.tools import assert_raises, eq_, eq_
class TestingCircleCreation:
    # Define a nose test method 'test_creating_circle_with_numeric_radius', which creates a circle with radius 2.5, and check if its radius value is 2.5 using eq_ method.
    def test_creating_circle_with_numeric_radius(self):
      c = Circle(2.5)
      eq_(2.5, c.radius)

    # Define a nose test method 'test_creating_circle_with_negative_radius', which checks if the ValueError exception is raised with the error message "radius must be between 0 and 1000 inclusive" using eq_ method, while creating a circle of radius -2.5.
    # Hint: Use assert_raises and with.
    def test_creating_circle_with_negative_radius(self):
      with assert_raises(ValueError) as e:
        c = Circle(-2.5)
      eq_(str(e.exception), 'radius must be between 0 and 1000 inclusive')

    # Define a nose test method 'test_creating_circle_with_greaterthan_radius', which checks if the ValueError exception is raised with the error message "radius must be between 0 and 1000 inclusive" using eq_ method, while creating circle of radius 1000.1 .
    # Hint: Use assert_raises and with
    def test_creating_circle_with_greaterthan_radius(self):
      with assert_raises(ValueError) as e:
        c = Circle(1000.1)
      eq_(str(e.exception), 'radius must be between 0 and 1000 inclusive')

    # Define a nose test method 'test_creating_circle_with_nonnumeric_radius', which checks if the TypeError exception is raised with the error message "radius must be a number" using eq_ method, while creating circle of radius 'hello' .
    # Hint: Use assert_raises and with.
    def test_creating_circle_with_nonnumeric_radius(self):
        with assert_raises(TypeError) as e:
          c = Circle('hello')
        eq_(str(e.exception), 'radius must be a number')


#Define a nose test class 'TestCircleArea'
class TestCircleArea:
    # Define a nose test method 'test_circlearea_with_random_numeric_radius', which creates a circle 'c1' with radius 2.5, and check if its computed area is 19.63 using eq_ method.
    def test_circlearea_with_random_numeric_radius(self):
        c = Circle(2.5)
        eq_(c.area(), 19.63)

    # Define a nose test method 'test_circlearea_with_min_radius', which creates a circle 'c2' with radius 0, and check if its computed area is 0 using eq_ method.
    def test_circlearea_with_min_radius(self):
        c = Circle(0)
        eq_(c.area(), 0)

    # Define a nose test method 'test_circlearea_with_max_radius', which creates a circle 'c3' with radius 1000, and check if its computed area is 3141592.65 using eq_ method.
    def test_circlearea_with_max_radius(self):
        c3 =Circle(1000)
        eq_(c3.area(), 3141592.65)

# Define a nose test class 'TestCircleCircumference'
class TestCircleCircumference:
    # Define a nose test method 'test_circlecircum_with_random_numeric_radius', which creates a circle 'c1' with radius 2.5, and check if its computed circumference is 15.71 using eq_ method.
    def test_circlecircum_with_random_numeric_radius(self):
        c = Circle(2.5)
        eq_(c.circumference(), 15.71)

    # Define a nose test method 'test_circlecircum_with_min_radius', which creates a circle 'c2' with radius 0, and check if its computed circumference is 0 using eq_ method.
    def test_circlecircum_with_min_radius(self):
        c = Circle(0)
        eq_(0, c.circumference())
    # Define a nose test method 'test_circlecircum_with_max_radius', which creates a circle 'c3' with radius 1000, and check if its computed circumference is 6283.19 using eq_ method.
    def test_circlecircum_with_max_radius(self):
        c = Circle(1000)
        eq_(c.circumference(), 6283.19)

3.Welcome to Python Qualis - Pytest Testing Framework

import sys
import os
sys.path.append(os.getcwd())
from proj.inventory import MobileInventory, InsufficientException
import pytest

# Import MobileInventory class and InsufficientException from the inventory module using the expression from proj.inventory import MobileInventory, InsufficientException.
# Import pytest using the expression import pytest.
# Use assert statement for assert, and to check. Ex: assert 1 == 1

# Define a pytest test class **'TestingInventoryCreation'**
class TestingInventoryCreation:

    # Define a pytest test method **'test_creating_empty_inventory'**, which creates an empty inventory and checks if its 'balance_inventory' is an empty dict using assert.
    def test_creating_empty_inventory(self):
        x = MobileInventory()
        assert x.balance_inventory == {}

    # Define a pytest test method **'test_creating_specified_inventory'**, which checks if inventory instance with input {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}.
    def test_creating_specified_inventory(self):
        x = MobileInventory({'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25})
        assert {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25} == x.balance_inventory

    # Define a pytest test method  **'test_creating_inventory_with_list'**, which checks if the  method raises a TypeError with the message "Input inventory must be a dictionary" when a list "['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z']" is passed as input using assert.
    def test_creating_inventory_with_list(self):
        with pytest.raises(TypeError) :
          assert "Input inventory must be a dictionary" == MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])

    # Define a pytest test method **'test_creating_inventory_with_numeric_keys'**, which checks if the  method raises a ValueError with the message "Mobile model name must be a string" using assert, when the dict {1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia Model Z'} is passed as input.
    def test_creating_inventory_with_numeric_keys(self):
        with pytest.raises(ValueError):
          assert "Mobile model name must be a string" == MobileInventory({1: 'iPhone Model X', 2: 'Xiaomi Model Y', 3: 'Nokia Model Z'})

    # Define a pytest test method **'test_creating_inventory_with_nonnumeric_values'**, which checks if the  method raises a ValueError with the message "No. of mobiles must be a positive integer" using assert, when the dict {'iPhone Model X':'100', 'Xiaomi Model Y': '1000', 'Nokia Model Z':'25'} is passed as input.
    def test_creating_inventory_with_nonnumeric_values(self):
        with pytest.raises(ValueError) :
          assert "No. of mobiles must be a positive integer" == MobileInventory({ 'iPhone Model X':'100', 'Xiaomi Model Y': '1000', 'Nokia Model Z':'25'})

    # Define a pytest test method **'test_creating_inventory_with_negative_value'**, which checks if the  method raises a ValueError with the message "No. of mobiles must be a positive integer" using assert, when the dict {'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia Model Z':25} is passed as input.
    def test_creating_inventory_with_negative_value(self):
        with pytest.raises(ValueError):
          assert "No. of mobiles must be a positive integer" == MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia Model Z':25})

# Define another pytest test class **'TestInventoryAddStock'**, which tests the behavior of the **'add_stock'** method, with the following tests
class TestInventoryAddStock:

    # Define a pytest class fixture 'setup_class', which creates an **'MobileInventory'** instance with input {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25} and assign it to class attribute **'inventory'**.
    inventory = None
    @classmethod
    def setup_class(cls):
        cls.inventory = MobileInventory({'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25})

    # Define a pytest test method **'test_add_new_stock_as_dict'**, which adds the new stock {'iPhone Model X':50, 'Xiaomi Model Y': 2000, 'Nokia Model A':10} to the existing inventory, and update the **balance_inventory** attribute. Also, check if the updated **balance_inventory** equals {'iPhone Model X':150, 'Xiaomi Model Y': 3000, 'Nokia Model Z':25, 'Nokia Model A':10} using assert.
    def test_add_new_stock_as_dict(self):
        self.inventory.add_stock({'iPhone Model X': 50, 'Xiaomi Model Y': 2000, 'Nokia Model A': 10})
        assert {'iPhone Model X':150, 'Xiaomi Model Y': 3000, 'Nokia Model Z':25,'Nokia Model A':10} == self.inventory.balance_inventory

    # Define a pytest test method **'test_add_new_stock_as_list'**, which adds the new stock ['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'] to the existing inventory, and which checks if the method raises a TypeError with the message "Input stock must be a dictionary" using assert.
    def test_add_new_stock_as_list(self):
        with pytest.raises(TypeError):
          assert "Input stock must be a dictionary" == MobileInventory.add_stock(self, ['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])

    # Define a pytest test method **'test_add_new_stock_with_numeric_keys'**, which adds the new stock {1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'} to the existing inventory, and which checks if the method raises a ValueError with the message "Mobile model name must be a string" using assert.
    def test_add_new_stock_with_numeric_keys(self):
        with pytest.raises(ValueError):
          assert "Mobile model name must be a string" == MobileInventory.add_stock(self, {1: 'iPhone Model A', 2: 'Xiaomi Model B',3: 'Nokia Model C'})

    # Define a pytest test method **'test_add_new_stock_with_nonnumeric_values'**, which adds the new stock {'iPhone Model A':'50', 'Xiaomi Model B':'2000', 'Nokia Model C':'25'} to the existing inventory, and which checks if the method raises a ValueError with the message "No. of mobiles must be a positive integer" using assert.
    def test_add_new_stock_with_nonnumeric_values(self):
        with pytest.raises(ValueError):
          assert "No. of mobiles must be a positive integer" == MobileInventory.add_stock(self, {'iPhone Model A': '50', 'Xiaomi Model B': '2000', 'Nokia Model C': '25'})

    # Define a pytest test method **'test_add_new_stock_with_float_values'**, which adds the new stock {'iPhone Model A':50.5, 'Xiaomi Model B':2000.3, 'Nokia Model C':25} to the existing inventory, and which checks if the method raises a ValueError with the message "No. of mobiles must be a positive integer" using assert.
    def test_add_new_stock_with_float_values(self):
        with pytest.raises(ValueError):
          assert "No. of mobiles must be a positive integer" == MobileInventory.add_stock(self, {'iPhone Model A': 50.5, 'Xiaomi Model B': 2000.3, 'Nokia Model C': 25})

# Define another pytest test class **'TestInventorySellStock'**, which tests the behavior of the **'sell_stock'** method, with the following tests
class TestInventorySellStock:
    
    # Define a pytest class fixture 'setup_class', which creates an **'MobileInventory'** instance with the input {'iPhone Model A':50, 'Xiaomi Model B': 2000, 'Nokia Model C':10, 'Sony Model D':1}, and assign it to the class attribute **'inventory'**.
    inventory = None
    @classmethod
    def setup_class(cls):
        cls.inventory = MobileInventory({ 'iPhone Model A': 50, 'Xiaomi Model B': 2000, 'Nokia Model C': 10, 'Sony Model D': 1})

    # Define a pytest test method **'test_sell_stock_as_dict'**, which sells the requested stock {'iPhone Model A':2, 'Xiaomi Model B':20, 'Sony Model D':1} from the existing inventory, and update the **balance_inventory** attribute. Also check if the updated **balance_inventory** equals {'iPhone Model A':48, 'Xiaomi Model B': 1980, 'Nokia Model C':10, 'Sony Model D':0} using assert.
    def test_sell_stock_as_dict(self):
        self.inventory.sell_stock({'iPhone Model A': 2, 'Xiaomi Model B': 20, 'Sony Model D': 1})
        assert {'iPhone Model A':48, 'Xiaomi Model B': 1980, 'Nokia Model C':10, 'Sony Model D':0} == self.inventory.balance_inventory

    # Define a pytest test method **'test_sell_stock_as_list'**, which tries selling the requested stock ['iPhone Model A', 'Xiaomi Model B', 'Nokia Model C'] from the existing inventory, and which checks if the method raises a TypeError with the message "Requested stock must be a dictionary" using assert.
    def test_sell_stock_as_list(self):
        with pytest.raises(TypeError):
          assert "Requested stock must be a dictionary" == MobileInventory.sell_stock(self, ['iPhone Model A', 'Xiaomi Model B', 'Nokia Model C'])

    # Define a pytest test method **'test_sell_stock_with_numeric_keys'**, which tries selling the requested stock {1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'} from the existing inventory, and which checks if the method raises ValueError with the message "Mobile model name must be a string" using assert.
    def test_sell_stock_with_numeric_keys(self):
        with pytest.raises(ValueError):
          assert "Mobile model name must be a string" == MobileInventory.sell_stock(self, {1: 'iPhone Model A', 2: 'Xiaomi Model B', 3: 'Nokia Model C'})

    # Define a pytest test method **'test_sell_stock_with_nonnumeric_values'**, which tries selling the requested stock {'iPhone Model A':'2', 'Xiaomi Model B':'3', 'Nokia Model C':'4'} from the existing inventory, and which checks if the method raises a ValueError with the message "No. of mobiles must be a positive integer" using assert.
    def test_sell_stock_with_nonnumeric_values(self):
        with pytest.raises(ValueError):
          assert "No. of mobiles must be a positive integer" == MobileInventory.sell_stock(self, {'iPhone Model A': '2', 'Xiaomi Model B':'3', 'Nokia Model C': '4'})

    # Define a pytest test method **'test_sell_stock_with_float_values'**, which tries selling the requested stock {'iPhone Model A':2.5, 'Xiaomi Model B':3.1, 'Nokia Model C':4} from the existing inventory, and which checks if the method raises a ValueError with the message "No. of mobiles must be a positive integer" using assert.
    def test_sell_stock_with_float_values(self):
        with pytest.raises(ValueError):
          assert "No. of mobiles must be a positive integer" == MobileInventory.sell_stock(self, {'iPhone Model A': 2.5, 'Xiaomi Model B': 3.1, 'Nokia Model C': 4})

    # Define a pytest test method **'test_sell_stock_of_nonexisting_model'**, which tries selling the requested stock {'iPhone Model B':2, 'Xiaomi Model B':5} from the existing inventory, and which checks if the method raises an InsufficientException with the message "No Stock. New Model Request" using assert.
    def test_sell_stock_of_nonexisting_model(self):
        with pytest.raises(InsufficientException):
          assert "No Stock. New Model Request" == MobileInventory.sell_stock(self.inventory, {'iPhone Model B': 2, 'Xiaomi Model B': 5})

    # Define a pytest test method **'test_sell_stock_of_insufficient_stock'**, which tries selling the requested stock {'iPhone Model A':2, 'Xiaomi Model B':5, 'Nokia Model C': 15} from the existing inventory, and which checks if the method raises an InsufficientException with the message "Insufficient Stock" using assert.
    def test_sell_stock_of_insufficient_stock(self):
        with pytest.raises(InsufficientException):
          assert "Insufficient Stock" == MobileInventory.sell_stock(self.inventory, {'iPhone Model A': 2, 'Xiaomi Model B': 5, 'Nokia Model C': 15})  

4.Unittest 


Unittest 1

import inspect
import re
import unittest
import math

# Define class 'Circle' and its methods with proper doctests:
class Circle:
    
    def __init__(self, radius):
        # Define initialization method:
        if not isinstance(radius, (int, float)):
                raise TypeError("radius must be a number")
        if not 1000 >=radius >=0:
            raise ValueError("radius must be between 0 and 1000 inclusive"
)
        self.radius=radius
        
    def area(self):
        # Define area functionality:
        return math.pi*(self.radius**2),2       
    def circumference(self):
        # Define circumference functionality:
        return math.pi*(self.radius*2),2

        
class TestCircleCreation(unittest.TestCase):

    def test_creating_circle_with_numeric_radius(self):
        # Define a circle 'c1' with radius 2.5, and check if 
        # the value of c1.radius is equal to 2.5 or not.
        c1 = Circle(2.5)
        self.assertEqual(c1.radius, 2.5)

    def test_creating_circle_with_negative_radius(self):
        # Define a circle 'c' with radius -2.5, and check 
        # if it raises a ValueError with the message
        # "radius must be between 0 and 1000 inclusive".
        with self.assertRaises(ValueError) as e:
            c = Circle(-2.5)
        self.assertEqual(str(e.exception),"radius must be between 0 and 1000 inclusive")

    def test_creating_circle_with_greaterthan_radius(self):
        # Define a circle 'c' with radius 1000.1, and check 
        # if it raises a ValueError with the message
        # "radius must be between 0 and 1000 inclusive".        
        with self.assertRaises(ValueError) as e:
            c = Circle(1000.1)
        self. assertEqual(str(e.exception),"radius must be between 0 and 1000 inclusive")

    def test_creating_circle_with_nonnumeric_radius(self):
        # Define a circle 'c' with radius 'hello' and check 
        # if it raises a TypeError with the message
        # "radius must be a number".        
        with self.assertRaises (TypeError) as e:
            c = Circle('hello')
        self.assertEqual(str(e.exception),"radius must be a number")


if __name__ == '__main__':   

Unittest 2

 
import inspect
import re
import unittest
import math

class Circle:
    
    def __init__(self, radius):
        # Define initialization method:
        if not isinstance(radius, (int, float)):
            raise TypeError("radius must be a number")
        if not 1000 >=radius >=0:
            raise ValueError("radius must be between 0 and 1000 inclusive"
)
        self.radius=radius
    def area(self):
        # Define area functionality:
         return round(math.pi*(self.radius**2),2)  
    def circumference(self):
        # Define circumference functionality:
        return round(math.pi*(self.radius*2),2)
class TestCircleArea(unittest.TestCase):
    
    def test_circlearea_with_random_numeric_radius(self):
        # Define a circle 'c1' with radius 2.5, and check if 
        # its area is 19.63.
        c1 = Circle(2.5)
        self.assertEqual(c1.area(), 19.63)
    def test_circlearea_with_min_radius(self):
        # Define a circle 'c2' with radius 0, and check if 
        # its area is 0.
        c1 = Circle(0)
        self.assertEqual(c1.area(), 0)
    def test_circlearea_with_max_radius(self):
        # Define a circle 'c3' with radius 1000.1. and check if 
        # its area is 3141592.65.
        c1 = Circle(1000)
        self.assertEqual(c1.area(), 3141592.65)
if __name__ == '__main__':  
Unittest 3

import inspect
import re
import unittest
import math
# Define class 'Circle' and its methods:
class Circle:
    
    def __init__(self, radius):
        # Define initialization method:
        if not isinstance(radius, (int, float)):
            raise TypeError("radius must be a number")
        if not 1000 >=radius >=0:
            raise ValueError("radius must be between 0 and 1000 inclusive"
)
        self.radius=radius
        
    def area(self):
        # Define area functionality:
        return round(math.pi*(self.radius**2),2)         
    def circumference(self):
        # Define circumference functionality:
        return round(math.pi*(self.radius*2),2)
class TestCircleCircumference(unittest.TestCase):
    
    def test_circlecircum_with_random_numeric_radius(self):
        # Define a circle 'c1' with radius 2.5, and check if 
        # its circumference is 15.71.
        c1 = Circle(2.5)
        self.assertEqual(c1.circumference(), 15.71)
    def test_circlecircum_with_min_radius(self):
        # Define a circle 'c2' with radius 0, and check if 
        # its circumference is 0.
        c1 = Circle(0)
        self.assertEqual(c1.circumference(), 0)
    def test_circlecircum_with_max_radius(self):
        # Define a circle 'c3' with radius 1000, and check if 
        # its circumference is 6283.19.
        c1 = Circle(1000)
        self.assertEqual(c1.circumference(), 6283.19)
if __name__ == '__main__':     
If you want answers to any of the fresco play courses feel free to ask in the comment section, we will surely help.

3 comments

  1. doctest 2 is not working
    1. try below one it's working

      class Circle:

      def __init__(self, radius):
      # Define the doctests for __init__ method below
      """
      >>> c1 = Circle(2.5)
      >>> c1.radius
      2.5
      """
      self.radius = radius

      def area(self):
      # Define the doctests for area method below
      """
      >>> c1 = Circle(2.5)
      >>> c1.area()
      19.63
      """
      # Define the area functionality below
      return round(((self.radius ** 2) * math.pi),2)


      def circumference(self):
      # Define the doctests for circumference method below
      """
      >>> c1 = Circle(2.5)
      >>> c1.circumference()
      15.71
      """
      # Define the circumference functionality below
      return round((self.radius * 2 * math.pi),2)
  2. not working this one
Any comments and suggestion will be appreciated.
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.

Join Telegram Channel

Join Notes Bureau Lets Teach and Learn

Join Telegram Channel
CLOSE ADS
CLOSE ADS