Skip to content

Commit 2cc860d

Browse files
committed
add solutions and practice oop
1 parent 17f1fdd commit 2cc860d

8 files changed

Lines changed: 71 additions & 6 deletions

File tree

javascript/LeetCode/math/3658.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 3658. GCD of Odd and Even Sums
3+
*
4+
* GCD 是 最大公因數(Greatest Common Divisor)的縮寫,指的是能夠整除兩個或以上非零整數的最大正整數。 例如,8和12的最大公因數是4,因為4是8和12的公因數中最大的那個
5+
*
6+
* sumOdd = 從1開始,n個奇數的總和
7+
* sumEven = 從1開始,n個偶數總和
8+
* gcd(sumOdd,sumEven) = answer
9+
*
10+
* @param {number} n
11+
* @return {number}
12+
*/
13+
var gcdOfOddEvenSums = function(n) {
14+
// ans 能夠整除sumOdd & sumEven
15+
// let sumOdd = [],sumEven = [];
16+
// for(let i = 1;i <= n*2;i++) {
17+
// if(i % 2 === 0 && sumOdd.length <= n){
18+
// sumEven.push(i);
19+
// }else{
20+
// sumOdd.push(i);
21+
// }
22+
// }
23+
// let odd = sumOdd.reduce((a,b)=>a+b,0);
24+
// let even = sumEven.reduce((a,b)=>a+b,0);
25+
// return Math.abs(odd-even);
26+
27+
// solution 2
28+
let sumEven = n * (n + 1);
29+
let sumOdd = n * n;
30+
return Math.abs(sumOdd - sumEven);
31+
};
32+
let n = 4;
33+
// 4
34+
// Sum of the first 4 odd numbers sumOdd = 1 + 3 + 5 + 7 = 16
35+
// Sum of the first 4 even numbers sumEven = 2 + 4 + 6 + 8 = 20
36+
// Hence, GCD(sumOdd, sumEven) = GCD(16, 20) = 4.
37+
console.log(gcdOfOddEvenSums(n))

python/index.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
# from file name(module) import the class.
2626
from oop.Animals import Animals
2727
from oop.Tortoises import Tortoises
28+
from oop.Lion import Lion
2829

2930
greek = Tortoises("福氣","7個月大","veggie","地中海型陸龜")
3031
greek.eat()
3132
greek.environment()
3233

34+
lion = Lion("獅子","未知","肉食")
35+
lion.environment()
36+
3337
# from oop.Fruit import Fruit
3438
# from oop.Melon import Melon
3539
# fruits = Fruit("Apple",3,5)

python/oop/Animals.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ def eat(self):
1111
print(f"{self.name}","is",f"{self.food}")
1212

1313
def environment(self):
14-
print("it's depends on specice")
14+
print("依據不同物種來評估適合的環境")
1515

1616
def get_sick(self):
1717
self.__health_level -= 20
18-
print(f"{self.name} 的健康值下降了。")
18+
print(f"{self.name} 的健康值下降了。")
19+
20+

python/oop/Lion.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from oop.Animals import Animals
2+
3+
class Lion(Animals):
4+
def __init__(self, name, age, food):
5+
super().__init__(name, age, food)
6+
7+
def eat(self):
8+
return super().eat()
9+
10+
def environment(self):
11+
print("溫暖")

python/oop/Tortoises.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,27 @@ def __init__(self, name, age, food,speciceType:str):
99
def eat(self):
1010
return super().eat()
1111

12+
13+
# 覆寫父類別的方法
1214
def environment(self):
13-
env = {"沙漠型陸龜":"沙漠型陸龜需要較高的溫度熱點",
14-
"地中海型陸龜":"有冬眠習性",
15+
'''
16+
環境
17+
'''
18+
env = {"沙漠型陸龜":"需要較高的溫度熱點",
19+
"地中海型陸龜":"有冬眠習性,但會因為所在地溫度而不冬眠",
1520
"雨林型陸龜":"需要較高的濕度",
1621
"海島型陸龜":"需要較高的濕度"
1722
}
1823

1924
for key, value in env.items():
20-
if key in self.speciceType:
21-
print(f"{key}: {value}")
25+
try:
26+
if key in self.speciceType:
27+
print(f"{key}: {value}")
28+
except NameError:
29+
print("eror")
2230

2331
def hibernation(self):
32+
'''
33+
冬眠
34+
'''
2435
print("if it's really cold")
22 Bytes
Binary file not shown.
939 Bytes
Binary file not shown.
205 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)