uaveiro-leci/1ano/fp/aula03/ex02.md

55 lines
1.4 KiB
Markdown

#### Exercícios propostos no [CodeCheck](https://horstmann.com/codecheck/index.html)
___
## Ex 1.
height = 4.5<br />
width = 3<br />
volume1 = balloonVolume(width, height)<br />
print("Original volume: ", volume1)<br />
volume2 = balloonVolume(width + 1, height + 1)<br />
change = volume2 - volume1<br />
print("Change: ", change)<br />
change = balloonVolume(width + 2, height + 2) - volume2<br />
print("Change: ", change)
___
## Ex 2.
def balloonVolume(width, height):<br />
&emsp;pi = 3.1415926<br />
&emsp;a = height / 2<br />
&emsp;c = width / 2<br />
&emsp;volume = 4 * pi * a * c * c / 3<br />
&emsp;return volume
___
## Ex 3.
def hideCharacters(string) :<br />
&emsp;return "*" * len(string)
___
## Ex 4.
def isEven(n):<br />
&emsp;if n % 2 == 0:<br />
&emsp;&emsp;return True<br />
&emsp;else:<br />
&emsp;&emsp;return False<br />
<br />
def main() :<br />
&emsp;page = int(input("Enter page number: "))<br />
&emsp;if isEven(page) :<br />
&emsp;&emsp;print(page)<br />
&emsp;else :<br />
&emsp;&emsp;print("%60d" % page)<br />
<br />
main()
___
## Ex 5.
def countSpaces(string):<br />
&emsp;spaces = 0<br />
&emsp;for char in string:<br />
&emsp;&emsp;if char == " ":<br />
&emsp;&emsp;&emsp;spaces += 1<br />
&emsp;return spaces<br />
<br />
def main() :<br />
&emsp;userInput = input("Enter a string: ")<br />
&emsp;spaces = countSpaces(userInput)<br />
&emsp;print(spaces)<br />
<br />
main()