Schreiben Sie ein Programm, das mit Hilfe von Dictionary Comprehensions eine Auflistung der drei am längsten aktiv gewesenen Rennpferden: {‚Pferdname1‘: n1, ‚Pferdname2‘: n2, ‚Pferdname3‘: n3}
In this challenge, you need to implement a method that squares passing variables and returns their sum. Problem statement
Implement a class Point that has three properties and a method. All these attributes (properties and methods) should be public. This problem can be broken down into two tasks: Task 1
Implement a constructor to initialize the values of three properties: x, y, and z. Task 2
Implement a method, sqSum(), in the Point class which squares x, y, and z and returns their sum.
Sample properties: 1, 3, 5
Sample method output: 35 Coding exercise
First, take a close look at the slides above and then design a similar step-by-step algorithm before jumping to its implementation.
1 2 3 4 5 6 7 8
class Point: def__init__(self): self.x= x self.y= y self.z= z
def sqSum(self): pass
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Point: def__init__(self, x, y, z): self.x= x self.y= y self.z= z
def sqSum(self):
a =self.x * self.x
b =self.y * self.y
c =self.z * self.z return(a + b + c)
obj1 = Point(4,5,6) print(obj1.sqSum())
Explanation
Task 1:
We passed parameters and assigned them to their corresponding properties.
Task 2:
We calculated the squares of the properties by multiplying them with itself and stored them in a variable.
Then, we added all the squared numbers.
Finally, we returned their sum
In this challenge, you need to implement a method that squares passing variables and returns their sum. Problem statement
In this exercise, you have to calculate a student’s total marks using the concept of Classes. Problem statement
Implement a class – Student – that has four properties and two methods. All these attributes (properties and methods) should be public. This problem can be broken down into three tasks. Task 1
Implement a constructor to initialize the values of four properties: name, phy, chem, and bio. Task 2
Implement a method – totalObtained – in the Student class that calculates total marks of a student.
Sample properties:
1 2 3 4
name = Mark
phy =80
chem =90
bio =40
Sample method output: obj1.Total()=210 Task 3
Using the totalObtained method, implement another method, percentage, in the Student class that calculates the percentage of students marks. Assume that the total marks of each subject are 100. The combined marks of three subjects are 300.
The formula for calculating the percentage:
Sample input:
1 2 3
phy =80
chem =90
bio =40
Sample output: 70 Coding exercise Design a step-by-step algorithm before jumping to the implementation.
1 2 3 4 5 6 7 8 9
class Student: def__init__(self): pass
def totalObtained(self): pass
def percentage(self): pass
Solution
1 2 3 4 5 6 7 8 9 10 11 12
class Student: def__init__(self, name, phy, chem, bio): self.name= name self.phy= phy self.chem= chem self.bio= bio
Explanation
In line 3 – 6, we have initialized name, phy, chem, and bio as public properties in the class initializer.
In line 9, we have then defined the totalObtained() method. In this method, we added individual marks of all the subjects and returned the sum.
At last in line 12, we defined percentage(). In this method, we called totalObtained() and used its value to calculate the percentage.
300
300
is the total number of marks for a student.
In this exercise, you have to implement a calculator that can perform addition, subtraction, multiplication, and division. Problem statement Write a Python class called Calculator by completing the tasks below: Task 1 Initializer:
Implement an initializer to initialize the values of num1 and num2
Properties:
num1
num2 Task 2 Methods:
add() is a method that returns the sum of num1 and num2.
subtract() is a method that returns the subtraction of num1 from num2.
multiply() is a method that returns the product of num1 and num2.
divide() is a method that returns the division of num2 by num1.
Input:
Pass numbers (integers or floats) in the initializer.
Output:
addition, subtraction, division, and multiplication
Sample input:
Explanation
We have implemented the Calculator class, which has the two properties, num1 and num2.
In the initializer, at line 3 – 4, we have initialized both properties, num1 and num2.
In line 7, we implemented add(), a method that returns the sum, num1 + num2, of both properties.
In line 10, we implemented subtract(), a method that returns the subtraction of num1 from num2.
In line 13, we implemented multiply(), a method that returns the product, num2 x num1, of both properties.
In line 16, we implemented divide(), a method that returns the division of num2 by num1.
Schreiben Sie ein Programm zur vereinfachten Berechnung der Steuer. Der Anwender wird dazu aufgefordert, sein monatliches Gehalt einzugeben. Anschließend werden 18 % dieses Betrags berechnet und ausgegeben. Nutzen Sie die Datei gehaltberechnung.py. Die Ausgabe kann zum Beispiel wie folgt aussehen:
Geben Sie Ihr Gehalt in Euro ein:
2500
Es ergibt sich eine Steuer von 450.0 Euro
Schreiben Sie ein Programm zur Eingabe und Umrechnung von beliebigen Inch-Werten in Zentimeter. Speichern Sie das Programm in der Datei inch_in_cm.py. Rufen Sie das Programm auf, und testen Sie es. Die Ausgabe kann zum Beispiel wie folgt aussehen:
Bitte geben Sie den Inch-Wert ein: 3.5
3.5 Inch sind 8.89 cm
Schreiben Sie eine Funktion join(values, delimiter), die eine Liste von Strings
mit der übergebenen Trennzeichenfolge verbindet und als einen String zurückliefert.
Implementieren Sie dies ohne Verwendung spezieller Python-Funktionalität selbst.
Beispiel:
Lösung
Zur Umwandlung des gegebenen Texts durchlaufen wir diesen zeichenweise von vorne nach hinten. Das Ergebnis sammeln wir in einem neuen String.
Finden wir einen Vokal, fügen wir die übergebene Ersatzzeichenfolge ein, ansonsten
den Konsonanten (bzw. genauer das Originalzeichen, was auch eine Ziffer oder ein
Satzzeichen sein könnte):
1 2 3 4 5 6 7 8
def join(values, delimiter):
result ="" for i, current_value inenumerate(values):
result += current_value # Kein Trenner nach letztem Vorkommen if i <len(values) - 1:
result += delimiter return result
Python-Shortcut Das Zusammenfügen von Strings lässt sich mit der geeigneten
Funktion join()schön kompakt, verständlich und ohne Spezialbehandlung schreiben:
Schreiben Sie eine Funktion translate_vowel(text, replacement), die in
einem gegebenen Text alle Vokale durch ein Zeichen bzw. eine Zeichenfolge ersetzt.
Das kann man etwa für ein kleines Ratequiz nutzen oder aber um Wortähnlichkeiten
nur basierend auf den Konsonanten zu ermitteln.
Lösung
Zur Umwandlung des gegebenen Texts durchlaufen wir diesen zeichenweise von vorne nach hinten. Das Ergebnis sammeln wir in einem neuen String.
Finden wir einen Vokal, fügen wir die übergebene Ersatzzeichenfolge ein, ansonsten
den Konsonanten (bzw. genauer das Originalzeichen, was auch eine Ziffer oder ein
Satzzeichen sein könnte):
1 2 3 4 5 6 7 8 9 10
def translate_vowel(text, replacement):
translated ="" for letter in text: if is_vowel(letter):
translated += replacement else:
translated += letter return translated def is_vowel(letter): return letter in"AÄEIOÖUüaäeioöuü"
Schreiben Sie eine Funktion calc(m, n), die zwei Variablen vom Typ int multipliziert, das Produkt dann halbiert und den ganzzahligen Rest bezüglich der Division durch 7 ausgibt.
Beispiel:
Als kleiner Hinweis zur Erinnerung hier nochmals: Bei einer Ganzzahldivision wird der
Rest abgeschnitten, deswegen ergibt 25/2 als Ergebnis den Wert 12.
Lösung
Algorithmus Die Implementierung folgt einfach der mathematischen Operationen:
1 2
def calc(m, n): return m * n // 2 % 7
Statt des speziellen Operators // kann man auch eine Umwandlung des Ergebnisses
der einfachen Division in eine Ganzzahl durch einen Aufruf von int() vornehmen: