Ahoj, moc bych chtěla poprosit o pomoc, mam za úkol vytvořit v pythonu převodník morseovky (oboustranný). Poradil by mi někdo nějaký základ jak na to? Mockrát děkuju
Fórum › Python
Prosba o pomoc se prací v Pythonu

v javascriptu... a si myslim, ze prepsat to do pythonu uz pro tebe nemuze byt problem, ne?
<script>
morse2 = {
".-": "A", "-...": "B", "-.-.": "C", "-..": "D",
".": "E", "..-.": "F", "--.": "G", "....": "H",
"..": "I", ".---": "J", "-.-": "K", ".-..": "L",
"--": "M", "-.": "N", "---": "O", ".--.": "P",
"--.-": "Q", ".-.": "R", "...": "S", "-": "T",
"..-": "U", "...-": "V", ".--": "W", "-..-": "X",
"-.--": "Y", "--..": "Z",
"-----": "0", ".----": "1", "..---": "2", "...--": "3", "....-": "4",
".....": "5", "-....": "6", "--...": "7", "---..": "8", "----.": "9"
};
morse1 = {
"a": ".-", "b": "-...", "c": "-.-.", "d": "-..",
"e": ".", "f": "..-.", "g": "--.", "h": "....",
"i": "..", "j": ".---", "k": "-.-", "l": ".-..",
"m": "--", "n": "-.", "o": "---", "p": ".--.",
"q": "--.-", "r": ".-.", "s": "...", "t": "-",
"u": "..-", "v": "...-", "w": ".--", "x": "-..-",
"y": "-.--", "z": "--..", " ": " ",
"1": ".----", "2": "..---", "3": "...--", "4": "....-", "5": ".....",
"6": "-....", "7": "--...", "8": "---..", "9": "----.", "0": "-----"
}
out1 = "";
out2 = "";
str = "ahoj";
arr = str.split("");
for (i=0;i<arr.length;i++)
{out1 += (morse1[arr[i]] ? morse1[arr[i]] : '?')+"/";}
arr = out1.split("/");
for (i=0;i<arr.length;i++)
{out2 += (morse2[arr[i]] ? morse2[arr[i]] : '?')+"";}
alert(str+"\n"+out1+"\n"+out2);
</script>
out1 += (morse1[arr[i]] ? morse1[arr[i]] : '?')+"/";
To je podminka pro existenci znaku v poli. kdyz neexistuje, tak error
if exist(morse1[arr[i]) out1+=morse1[arr[i] else out1+='?'
str.split("");
To je rozdeleni retezce do pole podle znaku. Cili treba 'a,b,c,d'.split(","); udela pole ['a','b','c','d']. Tusim, ze split je i v pythonu. V php se to jmenuje treba explode a nebo by tam slo pouzit preg_replace_callback / preg_match.
Nebo muzes pouzit pro vyhledavani v poli cykly. Coz je teda pomalejsi nez pres indexy pole, ale slo by to pak prepsat jinak.
morse1 = "abcdefghijklmnopqrstuvwxyz 1234567890".split("");
morse2 = ".-|-...|-.-.|-..|.|..-.|--.|....|..|.---|-.-|.-..|--|-.|---|.--.|--.-|.-.|...|-|..-|...-|.--|-..-|-.--|--..| |.----|..---|...--|....-|.....|-....|--...|---..|----.|-----".split("|");
out1 = "";
out2 = "";
str = morse1.join("");
str +=str;
arr = str.split("");
for (i=0;i<arr.length;i++)
{
pos = -1;
for (j=0;j<morse1.length;j++)
{
if (morse1[j]==arr[i]) {pos = j; break;}
};
out1 += (pos>=0 ? morse2[pos] : '?') + "/";
}
arr = out1.split("/");
for (i=0;i<arr.length;i++)
{
pos = -1;
for (j=0;j<morse2.length;j++)
{
if (morse2[j]==arr[i]) {pos = j; break;}
};
out2 += (pos>=0 ? morse1[pos] : '?') + "";
}
alert(str+"\n"+out1+"\n"+out2);
Ahoj, jsem uplny zacatecnik v Pythonu... a tak jsem zacal s pocatky v IDLE python pod win 10 3.5 a ejhle.
Nejednodusi prikaz Print nefunguje :(((
Zde je ysledek pokusu:
>>> Print ("Ahoj")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
Print ("Ahoj")
NameError: name 'Print' is not defined
>>>
Poradte prosim co s tim, doufam, ze zakladni prikazy / funkce jsou standardne aktivni bez dalsich definic atd.?
Predem dekuji. D
#5 Kit
ahoj a dik... uz jsem zkousel par variant a dokonce kopii z nejakeho vzoru,ale ze t chce jak jsi napsal me dostalo.. A le moc dik. D
Ahoj, jsem uplny zacatecnik v Pythonu... a tak jsem zacal s pocatky v IDLE python pod win 10 3.5 a ejhle.
Nejednodusi prikaz Print nefunguje :(((
Zde je ysledek pokusu:
>>> Print ("Ahoj")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
Print ("Ahoj")
NameError: name 'Print' is not defined
>>>
Poradte prosim co s tim, doufam, ze zakladni prikazy / funkce jsou standardne aktivni bez dalsich definic atd.?
Predem dekuji. D
#5 Kit
Promin, dekoval jsem do sveho textu :( Takze jeste jednou dik.
Celkem me zaskocilo, ze u Pythonu se projevi takova chyba... dle procteni nejakych popisu Python jsem si myslel, ze neni tak citlivy na zapis jako C,ale jak vidim, tak to tak neni zcela :( Jdu badat.
Mej se fajn. D.

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Prosba o pomoc — založil Harryk
Prosba o pomoc — založil Smokie
Prosba o pomoc s třídami — založil radaplech
Prosba o pomoc se změnou databáze — založil Vasek
Pomoc se seminarni praci — založil Killer stc
Moderátoři diskuze