Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_alx_pcz
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
kurs_alx_pcz
Commits
c0fda8ec
Commit
c0fda8ec
authored
Nov 28, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
generowanie numerów
parent
4769b4b2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
generowanie_numerow.py
dzien4/zadania/generowanie_numerow.py
+49
-0
No files found.
dzien4/zadania/generowanie_numerow.py
0 → 100644
View file @
c0fda8ec
'''
Stosując wyrażenie listotwórcze (list comprehension) utwórz listę numerów telefonów
(każdy jako str) o postaci +4812300XYYY , gdzie X może być cyfrą od 1 do 5,
a na każdej pozycji oznaczonej Y może być dowolna cyfra.
Oblicz długość listy (powinno być 5000).
Następnie wypisz na ekran dziesięć losowo wybranych numerów z tej listy.
'''
numery
=
[
'+4812300'
+
str
(
x
)
+
str
(
y1
)
+
str
(
y2
)
+
str
(
y3
)
for
x
in
range
(
1
,
6
)
for
y1
in
range
(
0
,
10
)
for
y2
in
range
(
0
,
10
)
for
y3
in
range
(
0
,
10
)]
numery
=
[
f
'+4812300{x}{y1}{y2}{y3}'
for
x
in
range
(
1
,
6
)
for
y1
in
range
(
0
,
10
)
for
y2
in
range
(
0
,
10
)
for
y3
in
range
(
0
,
10
)]
# zamiast 3 oddzielne y w zakresie od 0 do 9, można użyć jednego w zakresie od 0 do 999
# :03 powoduje dopisanie zer na początku
numery
=
[
f
'+4812300{x}{y:03}'
for
x
in
range
(
1
,
6
)
for
y
in
range
(
0
,
1000
)]
# można też przecież użyć jednej liczby z zakresu od 1000 do 5999
# wtedy nawet zera wiodące nie są potrzbne
numery
=
[
f
'+4812300{x}'
for
x
in
range
(
1000
,
6000
)]
# print(numery)
print
(
'Rozmiar listy:'
,
len
(
numery
))
print
(
'Pierwsze 10 elementów:'
,
numery
[:
10
])
print
(
'Ostatni element:'
,
numery
[
-
1
])
# wypisz 10 losowych elementów
import
random
# sposób 1: w pętli 10 razy losuję pozycję i z tej pozycji odczytuję element. teoretycznie mogą się powtórzyć
print
(
'
\n
Losowe elementy:'
)
for
_
in
range
(
10
):
poz
=
random
.
randrange
(
5000
)
print
(
numery
[
poz
])
# sposób 2: gotowa funkcja sample (ew. choices; sample - losowanie bez powtórzeń, choices - z możliwością powtórzenia)
print
(
'
\n
Losowe elementy:'
)
for
numer
in
random
.
sample
(
numery
,
10
):
print
(
numer
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment