Hallo zusammen,
ich bin über LeetCode gestolpert und dort wird folgende Schreibweise bei der Funktion verwendet:
def longestCommonPrefix(self, strs: List[str]) -> str:
bedeutet "-> str" einfach nur, dass die Funktion einen Rückgabewert String haben muss, weil notwendig ist es ja nicht, da die Funktion ja auch ohne "-> str" läuft.
Ist das so richtig oder bin ich da am Holzpfad
Danke euch und ein schönes Wochenende noch!
LeetCode Schreibweise
- __blackjack__
- User
- Beiträge: 14330
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Das bedeutet das der Autor sagt die Methode hat `str`-Objekte als Rückgabewert. Die Python-Dokumentation zum Thema: https://docs.python.org/3/library/typing.html
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
- __blackjack__
- User
- Beiträge: 14330
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
Ich habe das mal in eine Suchmaschine geworfen und Lösungen in dieser Form gefunden:
Wenn *das* ”Python” ist, was man dort lernt, bitte eine andere Lernplattform suchen. Das ist Java in Python-Syntax, aber kein idiomatisches Python.
Code: Alles auswählen
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
longest = ""
for c in zip(*strs):
if len(set(c)) == 1:
longest += c[0]
else:
break
return longest„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
