Skip to content

Commit eba70c4

Browse files
authored
Merge pull request #148 from clingoram/mavis
python & js練習
2 parents 3f14118 + 2cc860d commit eba70c4

35 files changed

Lines changed: 283 additions & 133 deletions

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
2. 題目來源:
55
- LeetCode
66
- CodeWars
7+
- HackerRank
78

89
雖是JavaScript,但實際上使用Node.js,因此不需要打開瀏覽器便可執行JS的環境
9-
CMD打上node app.js
10+
CMD打上node {檔案名稱.js},EG.node index.js <br>
11+
而Python,則是打上 python3 {檔案名稱.py} EG.python3 index.py

javascript/LeetCode/Array/3683.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 3683. Earliest Time to Finish One Task
3+
*
4+
* task = [start time,finsh time]
5+
* 回傳task最早完成的時間
6+
* @param {number[][]} tasks
7+
* @return {number}
8+
*/
9+
var earliestTime = function(tasks) {
10+
let ans = Infinity;
11+
for(let i = 0;i < tasks.length;i++) {
12+
ans = Math.min(ans,tasks[i][0] + tasks[i][1]);
13+
}
14+
return ans;
15+
};
16+
let tasks = [[1,6],[2,3]];
17+
// 5
18+
// The first task starts at time t = 1 and finishes at time 1 + 6 = 7. The second task finishes at time 2 + 3 = 5. You can finish one task at time 5.
19+
console.log(earliestTime(tasks));

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))

javascript/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,8 +1024,7 @@ var closetPair = function(arr1,arr2,x) {
10241024
}
10251025

10261026
}
1027-
// let arr1 = [1,4,5,7],arr2 = [10,20,30,40],x = 32;
1027+
// let arr1 = [1,4,5,7],arr2 = [10,20,30,40],x = 32;
10281028
// [1,30];
10291029
// console.log(closetPair(arr1,arr2,x));
10301030

1031-

python/index.py

Lines changed: 14 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from math import sqrt
1111
from operator import le
1212
from typing import List
13+
import math
1314

1415

1516
# from numpy import diff
@@ -22,38 +23,29 @@
2223

2324
# oop
2425
# from file name(module) import the class.
26+
from oop.Animals import Animals
27+
from oop.Tortoises import Tortoises
28+
from oop.Lion import Lion
29+
30+
greek = Tortoises("福氣","7個月大","veggie","地中海型陸龜")
31+
greek.eat()
32+
greek.environment()
33+
34+
lion = Lion("獅子","未知","肉食")
35+
lion.environment()
36+
2537
# from oop.Fruit import Fruit
2638
# from oop.Melon import Melon
2739
# fruits = Fruit("Apple",3,5)
28-
# print(fruits.calculate())
40+
# print("價格:",f"{fruits.calculate()}")
2941

30-
# fruits = Fruit()
3142
# fruits.make_watering()
3243

33-
# v = Melon()
44+
# v = Melon("西瓜",65,10,30)
3445
# v.make_seedling()
3546
# v.palnt()
3647

3748

38-
39-
def diagonalDifference(arr):
40-
'''
41-
Diagonal Difference
42-
Complete the 'diagonalDifference' function below.
43-
44-
The function is expected to return an INTEGER.
45-
The function accepts 2D_INTEGER_ARRAY arr as parameter.
46-
'''
47-
left = 0
48-
right = 0
49-
for i in range(len(arr)):
50-
left += arr[i][i]
51-
right += arr[i][len(arr) - 1 - i]
52-
return abs(left - right)
53-
54-
55-
56-
5749
'''
5850
1415. The k-th Lexicographical String of All Happy Strings of Length n
5951
@@ -221,53 +213,6 @@ def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
221213
# print(a.getFinalState(nums,k,multiplier))
222214

223215

224-
225-
'''
226-
3407. Substring Matching Pattern
227-
228-
You are given a string s and a pattern string p, where p contains exactly one '*' character.
229-
The '*' in p can be replaced with any sequence of zero or more characters.
230-
Return true if p can be made a substring of s, and false otherwise.
231-
232-
Hints:
233-
1. Divide the pattern in two strings and search in the string.
234-
235-
Example 1:
236-
Input: s = "leetcode", p = "ee*e"
237-
Output: true
238-
Explanation:
239-
By replacing the '*' with "tcod", the substring "eetcode" matches the pattern.
240-
241-
Example 2:
242-
Input: s = "car", p = "c*v"
243-
Output: false
244-
Explanation:
245-
There is no substring matching the pattern.
246-
247-
Example 3:
248-
Input: s = "luck", p = "u*"
249-
Output: true
250-
Explanation:
251-
The substrings "u", "uc", and "uck" match the pattern.
252-
253-
Constraints:
254-
1 <= s.length <= 50
255-
1 <= p.length <= 50
256-
s contains only lowercase English letters.
257-
p contains only lowercase English letters and exactly one '*'
258-
259-
參數為一個字串s和字串p,p內有一個"*"符號,而該符號可被替換成任一或多個字母
260-
若p的*號在替換成字母後可變成s的子字串,則回傳true
261-
否則false
262-
'''
263-
def hasMatch(s: str, p: str) -> bool:
264-
# 將字串拆成兩部分再搜尋
265-
return False
266-
# s = "leetcode"
267-
# p = "ee*e"
268-
# True
269-
# print(hasMatch(s,p))
270-
271216
'''
272217
2523. Closest Prime Numbers in Range
273218
@@ -296,8 +241,6 @@ def hasMatch(s: str, p: str) -> bool:
296241
297242
Constraints:
298243
1 <= left <= right <= 106
299-
300-
301244
'''
302245
def closestPrimes(left: int, right: int) -> List[int]:
303246
'''
@@ -353,48 +296,3 @@ def isPrime(element:int):
353296
# print(closestPrimes(left,right))
354297

355298

356-
'''
357-
2873. Maximum Value of an Ordered Triplet I
358-
359-
You are given a 0-indexed integer array nums.
360-
Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
361-
The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
362-
363-
Hints:
364-
1.Use three nested loops to find all the triplets.
365-
366-
Example 1:
367-
Input: nums = [12,6,1,2,7]
368-
Output: 77
369-
Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
370-
It can be shown that there are no ordered triplets of indices with a value greater than 77.
371-
372-
Example 2:
373-
Input: nums = [1,10,3,4,19]
374-
Output: 133
375-
Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
376-
It can be shown that there are no ordered triplets of indices with a value greater than 133.
377-
Example 3:
378-
379-
Input: nums = [1,2,3]
380-
Output: 0
381-
Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
382-
383-
384-
Constraints:
385-
3 <= nums.length <= 100
386-
1 <= nums[i] <= 106
387-
388-
'''
389-
def maximumTripletValue(nums: List[int]) -> int:
390-
# i < j < k
391-
# (nums[i] - nums[j]) * nums[k]
392-
ans = 0
393-
394-
return ans
395-
396-
397-
nums = [12,6,1,2,7]
398-
# 77
399-
# (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
400-
print(maximumTripletValue(nums))

0 commit comments

Comments
 (0)