2023年6月17日 星期六

Python 的 Tuples 的一些事情

 python 中有個東東叫 Tuples ,用法跟 list 一樣。但不同的地方如下:

1. 元素可以混合,如 [ 1 ,2 ,3 ,"one" ,"two" ,"three"]

2. 元素皆唯讀不可修改刪除。

2022年12月3日 星期六

Visual Studio Code (vscode ) 的 console 顯示行數調整

 Visual Studio Code (vscode ) 的 console 顯示行數調整

預設顯示輸出是 1000 行,如果程式輸出超過 1000 行就會被洗掉了,

這時候就會想調整一下讓輸出變多一點。

解決方法如下 :

到 [ 檔案 > 喜好設定 > 設定 ]  or " Ctrl + , " 叫出 [ 設定 ] 


然後輸入 " Terminal › Integrated: Scrollback "

把預設 1000 改成 10000 就可以了喔 !

如下圖 :


大家可以試試看~


2022年9月28日 星期三

How to use Git & GitHub

以前一直想學一下怎麼用 GitHub 但就是沒用到就沒學了,最近有時間了就來學一下。

原來喔,常聽到 Git 什麼的就是專做版本控制的程式,我們會去安裝 Git 的程式來對檔案做版本控制。

而 GitHub 就只是專門儲存 Git 產生出來的那些檔案版本的線上儲存庫。

所以人家常說的要學版本控制就是要學 Git 這個程式的指令操作。而那些產生出來的版本檔案放在本機就不安全(硬碟會壞掉(抖)),然後專案要共享給很多人一起方便進行。

而 GitHub 就是線上服務,就想像它就是 Google Drive,然後完整的跟 Git 做整合了。

所以要用 GitHub 就要先學怎麼操作 Git 這個可以幫檔案做版本控制的程式。

另外就是目前透過 HTTP 服務把程式 PUSH 上去的時候不能透過帳號密碼驗證上傳了,要改用帳號+自己建立的 token 才能上傳。 

建立自己專屬的 token 方法可以參考這篇 :

https://docs.github.com/cn/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

啊除了HTTP上傳以外也可以用 SSH 喔

Leetcode 紀錄 - Palindrome Number

 Palindrome Number

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

For example, 121 is a palindrome while 123 is not.

這個問題是說會輸入一個數值,如果是一個對稱的數就回傳 true ,不是對稱的就還傳 false 。

我自己的處理辦法是將輸入的數值轉成字串處理XD (超懶 應該有更棒的處理辦法就是了

然後直接透過 for 加上 python 可以用負值的 index 去倒著讀取字串。

就可以直接拿字串第一個和最後一個做 sequence XD

class Solution:
    def isPalindrome(self, x: int) -> bool:
        for ind in range(len(str(x))-1) :
            if str(x)[ind] != str(x)[-ind -1] :
                return False
        return True           

x=12521
print(Solution.isPalindrome(Solution,x))

Python char to ascii code

 Python char to ascii code

X = 'A'
print(ord(X))

Y = 65
print(chr(Y))

Leetcode 紀錄 - Two sum

Two sum

在這個問題中會得到一個 target 和一個 array ,而 target 一定會是 array 中兩的數值的合,

我們要找出那兩個數值在 array 中的位置,然後還要符合時間複雜度。

坦白說我覺得滿難的QQ.... 實在是沒怎麼想過這類問題。希望可以越寫越熟拉 哈哈

總之,我自己是寫了兩個解法,感覺是很標準的那種沒什麼特別的就是了,

第一種是不管時間複雜度,直接暴力給他跑兩個 for,重點在於當第一個數值進來的時候會掃一次所以其他在 array 中的值直到找到另一個值相加等於 target 時就結束。

Python :

class Solution:
    def twoSum(self, nums, target: int) -> int:
        result_list=[]
        for nums_index in range(len(nums)) :
            for num_t in  range(len(nums)) :
                if  num_t != nums_index > 0 and nums[nums_index] + nums[num_t] == target :
                    print(nums[nums_index] ,nums[num_t])
                    result_list.append(nums_index)
                    result_list.append(num_t)                               
                    return result_list
        
nums = [7,3,9,3,78,8]
print(Solution.twoSum(Solution,nums,6))

另一個作法是只要一個 for 就好,時間複雜度是符合解題許可範圍的,但不是最佳解就是了。

總之,另一個作法會要先知道 python dictionary 的一些特性與用法就可以解了,

(當初不知道就想很久....有看到說可以用 key-value pairs 就會了XD)

Python :

class Solution:
	def twoSum(self, nums, target: int) -> int:
        result_dict = {}
        for nums_index in range(len(nums)) :
            print('nums index : ',nums_index)
            t_nums1 = nums[nums_index] 
            t_nums2 = target - t_nums1
            print(t_nums1)
            if t_nums2 in result_dict :  
                return [result_dict[t_nums2],nums_index]   
            result_dict[t_nums1] = nums_index
            # 流程上就是一個一個數值都跑一次 , 每跑一次不符合條件就直接依序加到 dict 中
nums = [7,3,9,3,78,8]
print(Solution.twoSum(Solution,nums,6))

裡面的 if 條件就是一直 try 到有 t_nums2 的值在 result_dict 中就會知道是第幾個了, 也會知道目前是在處理哪一個值。所以當有符合這個條件的時候就會回傳答案出來了。



Python print 文字時,文字與文字之間可以加一些符號

Python print 文字時,文字與文字之間可以加一些符號,感覺滿有趣的。

print(1, 2, 3, 4)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end='&')
print('I love {0} and {1}'.format('bread','butter'))
print('I love {1} and {0}'.format('bread','butter'))
#直接寫到檔案裡,滿方便der
with open('out.txt', 'a+') as f:
    print('1,2,3', sep='%' , file=f)