What's new

Help Pa help How to code this in Python

athanniel04

Honorary Poster
Joined
Apr 9, 2020
Posts
322
Reaction
76
Points
167
Write an interactive python program to append a new item to the end of the array.
use input() function.

2. Write an python program to generate 5 random integer elements of values 1-10 array, and print this.
use random.randint() to get the elements.

3. Write an python program to to sort the elements of an array in ascending order, print this and reverse
order.
use random.randrandint() to generate the elements to sort.
use the algorithm:
1. Generate 5 random integers of values 1-10.
2. Displaying elements generated.
3. Sort the array of elements in ascending order and display.
4. Sort in descending order using reverse function and display.

4. Write a Python program to count the number of each character of a given text of a text file.
import collections, ppri
 
Google or stockoverflow is your friend :)
Even I know how to code it, I can't help you because it's your assignment. You need at least to do some initial codes first, and then you can post the problem instead.

Kaya mo yan. Kung nagsisimula ka palang, much better to do some own research para mas gaganahan ka lalo na kapag nasosolve mo ang problem ng walang tulong ng iba.
 
Last edited:
Kuya okay lng ba ito para sa number 1?
l = ["Salad", "Grahams", "Coffee Jelly", "Cake"]
print(" List of Desserts:",l)

x = str(input("Please add another dessert you want: "))
l.append(str(x))

print("New list of Desserts:", l)
 
yes that's correct. Pero pwede mo iprint yung lists line by line like this

Python:
def print_array(l):
    print('--------------------')
    print('\n'.join(l))
    print('--------------------')

l = ["Salad", "Grahams", "Coffee Jelly", "Cake"]
print("List of Desserts:")
print_array(l)

x = str(input("Please add another dessert you want: "))
l.append(x)

print("New List of Desserts:")
print_array(l)
 
[XX='zackmark29, c: 1173275, m: 77455'][/XX]
Medyo nahirapan ako dito .. Yung sa part na generate 5 random integers
Kasi kapag ginamit ko ito
for _ in range(5):
value = randint(1, 10)
print(value)
Error daw kasi walang attribute si int na sort

Ito po code ko
#Generate 5 random integers of values 1-10.
import random
num1 = random.randint(1,10)
num2 = random.randint(1,10)
num3 = random.randint(1,10)
num4 = random.randint(1,10)
num5 = random.randint(1,10)
arr = [num1, num2, num3, num4, num5]
#Displaying elements generated.
print("Random Elements Generated: ",arr)
print()
#Sort the array of elements in ascending order and display.
arr.sort()
print("Sorted array of elements in ascending order:",arr)
print()
#Sort in descending order using reverse function and display.
arr.sort(reverse=True)
print("Sorted array of elements in descending order:",arr)
 
[XX='athanniel04, c: 1173754, m: 1700512'][/XX] ah baka wala kang package ng numpy. try mo research about sa eror sayo. haha ang haba ng ginawa mo sa taas ayan isang line lang =D
 
athanniel04

eto try mo without numpy

Python:
import random

max = 10
for _ in range(5):
      print(random.randint(0, max))

#OUTPUT 
1
6
2
4
9

or
Python:
max = 10
generated = []
for _ in range(5):
    generated.append(random.randint(0, max))
    
print(generated)

#OUTPUT
#[3, 3, 0, 7, 4]
 
Last edited:
para sa number 5 mo kung wala kapa. eto ginawan nalang kita ng function para reusable sa ibang project


Python:
def count_strings(file):
    num_of_lines = 0
    num_of_words = 0
    num_of_chars = 0

    with open(file, 'r') as f:
        lines = f.readlines()
        num_of_lines = len(lines)
        s = ''.join(lines).split()
        num_of_words = len(s)
        num_of_chars += sum(len(x) for x in s)

    return {
        'num_of_lines': str(num_of_lines),
        'num_of_words': str(num_of_words),
        'num_of_chars': str(num_of_chars)
    }

result = count_strings('test.txt')
print(f"number of lines: {result['num_of_lines']}")
print(f"number of words: {result['num_of_words']}")
print(f"number of characters: {result['num_of_chars']}")
 

Similar threads

Back
Top