What's new
  • Welcome to PHCorner forums. Take a moment to sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Help Pa help How to code this in Python

athanniel04

Journeyman
Joined
Apr 9, 2020
Posts
90
Reaction
14
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:
A
athanniel04

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)
 
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:
Comment
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']}")
 
A
athanniel04
Pano ba yung import collections..
 

Online statistics

Members online
1,466
Guests online
1,265
Total visitors
2,731

Forum statistics

Threads
1,113,428
Posts
18,533,311
Members
1,535,858
Comments
1,304,685
Attachments
1,466,517
Profile posts
98,455
Top