Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: floating_point_array.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script defines a class `Array` representing an array with insertion, removal, retrieval, and sorting operations.
- - It generates an array of random floating-point values within a specified range and size.
- Requirements:
- - Python 3.x
- Functions:
- - __init__(self, size):
- Initializes the array with random floating-point values.
- - popback(self):
- Removes the last element from the array.
- - get(self, i):
- Retrieves the value at the i-th index of the array.
- - insert(self, i, n):
- Inserts a value n at the i-th index of the array.
- - sort(self):
- Sorts the array in ascending order.
- Expected Example Output:
- --------------------------------------------------
- Initial Array:
- 0: 21.3756257549
- 1: 22.2067609966
- 2: 11.5291278413
- 3: 49.1549062161
- 4: 42.0711051408
- --------------------------------------------------
- Array after removing the last element:
- 0: 21.3756257549
- 1: 22.2067609966
- 2: 11.5291278413
- 3: 49.1549062161
- --------------------------------------------------
- Value at index 1:
- 0: 21.3756257549
- 1: 22.2067609966
- 2: 11.5291278413
- 3: 49.1549062161
- 4: 42.0711051408
- --------------------------------------------------
- Array after inserting 33.3333333333 at index 3:
- 0: 21.3756257549
- 1: 22.2067609966
- 2: 11.5291278413
- 3: 33.3333333333
- 4: 49.1549062161
- 5: 42.0711051408
- --------------------------------------------------
- Array after sorting:
- 0: 11.5291278413
- 1: 21.3756257549
- 2: 22.2067609966
- 3: 33.3333333333
- 4: 42.0711051408
- 5: 49.1549062161
- --------------------------------------------------
- Usage:
- - Create an instance of the `Array` class with a specified size.
- - Use various methods to manipulate the array, such as popping back elements, getting values at specific indices, inserting values, and sorting the array.
- """
- import random
- class Array:
- """
- A class representing an array with various operations.
- Attributes:
- capacity (int):
- The maximum capacity of the array.
- length (int):
- The current length of the array.
- arr (list):
- The list containing the elements of the array.
- Methods:
- __init__(self, size):
- Initializes the array with random floating-point values.
- popback(self):
- Removes the last element from the array.
- get(self, i):
- Gets the value at the i-th index of the array.
- insert(self, i, n):
- Inserts a value n at the i-th index of the array.
- sort(self):
- Sorts the array in ascending order.
- """
- def __init__(self, size):
- """
- Initializes the array with random floating-point values.
- Args:
- size (int): The size of the array.
- """
- self.capacity = size
- self.length = size
- self.arr = [round(random.uniform(1, 50), 10) for _ in range(size)]
- def popback(self):
- """
- Removes the last element from the array.
- """
- print("\nArray after removing the last element:\n")
- for i in range(self.length - 1):
- print(f"\t{i}: {self.arr[i]}")
- print("-" * 50)
- print()
- def get(self, i):
- """
- Gets the value at the i-th index of the array.
- Args:
- i (int): The index of the value to retrieve.
- """
- print(f"Value at index {i}:\n")
- for idx, val in enumerate(self.arr):
- print(f"\t{idx}: {val}")
- print("-" * 50)
- print()
- def insert(self, i, n):
- """
- Inserts a value n at the i-th index of the array.
- Args:
- i (int): The index at which to insert the value.
- n (float): The value to insert.
- """
- self.arr.insert(i, n)
- self.length += 1
- print(f"Array after inserting {n} at index {i}:\n")
- for idx, val in enumerate(self.arr):
- print(f"\t{idx}: {val}")
- print("-" * 50)
- print()
- def sort(self):
- """
- Sorts the array in ascending order.
- """
- self.arr.sort()
- print("Array after sorting:\n")
- for idx, val in enumerate(self.arr):
- print(f"\t{idx}: {val}")
- print("-" * 50)
- print()
- def main():
- print("-" * 50)
- size = 5 # Specify the size of the array
- my_array = Array(size)
- print("Initial Array:\n")
- for idx, val in enumerate(my_array.arr):
- print(f"\t{idx}: {val}")
- print("-" * 50)
- my_array.popback()
- my_array.get(1)
- my_array.insert(3, 33.3333333333) # Test value to insert in the array list
- my_array.sort()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement