Advertisement
Python253

floating_point_array

May 24th, 2024
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: floating_point_array.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script defines a class `Array` representing an array with insertion, removal, retrieval, and sorting operations.
  10.    - It generates an array of random floating-point values within a specified range and size.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.  
  15. Functions:
  16.    - __init__(self, size):
  17.        Initializes the array with random floating-point values.
  18.    - popback(self):
  19.        Removes the last element from the array.
  20.    - get(self, i):
  21.        Retrieves the value at the i-th index of the array.
  22.    - insert(self, i, n):
  23.        Inserts a value n at the i-th index of the array.
  24.    - sort(self):
  25.        Sorts the array in ascending order.
  26.  
  27. Expected Example Output:
  28.  
  29.    --------------------------------------------------
  30.    Initial Array:
  31.  
  32.        0: 21.3756257549
  33.        1: 22.2067609966
  34.        2: 11.5291278413
  35.        3: 49.1549062161
  36.        4: 42.0711051408
  37.    --------------------------------------------------
  38.  
  39.    Array after removing the last element:
  40.  
  41.        0: 21.3756257549
  42.        1: 22.2067609966
  43.        2: 11.5291278413
  44.        3: 49.1549062161
  45.    --------------------------------------------------
  46.  
  47.    Value at index 1:
  48.  
  49.        0: 21.3756257549
  50.        1: 22.2067609966
  51.        2: 11.5291278413
  52.        3: 49.1549062161
  53.        4: 42.0711051408
  54.    --------------------------------------------------
  55.  
  56.    Array after inserting 33.3333333333 at index 3:
  57.  
  58.        0: 21.3756257549
  59.        1: 22.2067609966
  60.        2: 11.5291278413
  61.        3: 33.3333333333
  62.        4: 49.1549062161
  63.        5: 42.0711051408
  64.    --------------------------------------------------
  65.  
  66.    Array after sorting:
  67.  
  68.        0: 11.5291278413
  69.        1: 21.3756257549
  70.        2: 22.2067609966
  71.        3: 33.3333333333
  72.        4: 42.0711051408
  73.        5: 49.1549062161
  74.    --------------------------------------------------
  75.  
  76. Usage:
  77.    - Create an instance of the `Array` class with a specified size.
  78.    - Use various methods to manipulate the array, such as popping back elements, getting values at specific indices, inserting values, and sorting the array.
  79. """
  80.  
  81. import random
  82.  
  83. class Array:
  84.     """
  85.    A class representing an array with various operations.
  86.  
  87.    Attributes:
  88.        capacity (int):
  89.            The maximum capacity of the array.
  90.        length (int):
  91.            The current length of the array.
  92.        arr (list):
  93.            The list containing the elements of the array.
  94.  
  95.    Methods:
  96.        __init__(self, size):
  97.            Initializes the array with random floating-point values.
  98.        popback(self):
  99.            Removes the last element from the array.
  100.        get(self, i):
  101.            Gets the value at the i-th index of the array.
  102.        insert(self, i, n):
  103.            Inserts a value n at the i-th index of the array.
  104.        sort(self):
  105.            Sorts the array in ascending order.
  106.    """
  107.  
  108.     def __init__(self, size):
  109.         """
  110.        Initializes the array with random floating-point values.
  111.  
  112.        Args:
  113.            size (int): The size of the array.
  114.        """
  115.         self.capacity = size
  116.         self.length = size
  117.         self.arr = [round(random.uniform(1, 50), 10) for _ in range(size)]
  118.  
  119.     def popback(self):
  120.         """
  121.        Removes the last element from the array.
  122.        """
  123.         print("\nArray after removing the last element:\n")
  124.         for i in range(self.length - 1):
  125.             print(f"\t{i}: {self.arr[i]}")
  126.         print("-" * 50)
  127.         print()
  128.  
  129.     def get(self, i):
  130.         """
  131.        Gets the value at the i-th index of the array.
  132.  
  133.        Args:
  134.            i (int): The index of the value to retrieve.
  135.        """
  136.         print(f"Value at index {i}:\n")
  137.         for idx, val in enumerate(self.arr):
  138.             print(f"\t{idx}: {val}")
  139.         print("-" * 50)
  140.         print()
  141.  
  142.     def insert(self, i, n):
  143.         """
  144.        Inserts a value n at the i-th index of the array.
  145.  
  146.        Args:
  147.            i (int): The index at which to insert the value.
  148.            n (float): The value to insert.
  149.        """
  150.         self.arr.insert(i, n)
  151.         self.length += 1
  152.         print(f"Array after inserting {n} at index {i}:\n")
  153.         for idx, val in enumerate(self.arr):
  154.             print(f"\t{idx}: {val}")
  155.         print("-" * 50)
  156.         print()
  157.  
  158.     def sort(self):
  159.         """
  160.        Sorts the array in ascending order.
  161.        """
  162.         self.arr.sort()
  163.         print("Array after sorting:\n")
  164.         for idx, val in enumerate(self.arr):
  165.             print(f"\t{idx}: {val}")
  166.         print("-" * 50)
  167.         print()
  168.  
  169. def main():
  170.     print("-" * 50)
  171.     size = 5  # Specify the size of the array
  172.     my_array = Array(size)
  173.  
  174.     print("Initial Array:\n")
  175.     for idx, val in enumerate(my_array.arr):
  176.         print(f"\t{idx}: {val}")
  177.     print("-" * 50)
  178.        
  179.     my_array.popback()
  180.     my_array.get(1)
  181.     my_array.insert(3, 33.3333333333)  # Test value to insert in the array list
  182.     my_array.sort()
  183.  
  184.  
  185. if __name__ == "__main__":
  186.     main()
  187.  
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement