What's new

# HelpPa help How to code this in Python

#### athanniel04

##### Journeyman
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

#### zackmark29

Elite
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:
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)

#### zackmark29

Elite
sa number 2 mo ganito siya

Python:
``````import numpy as np

print(np.random.randint(10, size=5))``````

#OUTPUT
#[4 9 3 6 3] random 5 digits

zackmark29
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

#### zackmark29

Elite
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

#### zackmark29

Elite
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:
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']}")``````

athanniel04
Pano ba yung import collections..

Replies
7
Views
181
Replies
9
Views
93
Replies
7
Views
119
C & C++ Multiple of 3
Replies
2
Views
99
Replies
1
Views
100

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