# Archive for category Uncategorized

### Reverse Integer

Posted by pttjuniper in Uncategorized on November 18, 2017

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123

Output: 321

Example 2:

Input: -123

Output: -321

Example 3:

Input: 120

Output: 21

Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

class Solution: def reverse(self, x): """ :type x: int :rtype: int """ list_x = [] if x >0 : for str_x in str(x).split(): list_x.append(str_x) r_list_x = list_x[::-1] for item in list_x: r_str_x = "".join(item) return (int (r_str_x))

What is wrong with this one:

x = 123,

str(x).split() is still “123”

split is split the spaces…

### two sum V3

Posted by pttjuniper in Uncategorized on November 18, 2017

class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dic_value_index = {} index_1 = int () index_2 = int () for index, value in enumerate(nums): dic_value_index [value] = index for item in nums: sec_num = target - item if sec_num in dic_value_index: if nums.index(item) != dic_value_index[sec_num]: # if they are not the same number index_1 = nums.index(item) index_2 = dic_value_index[sec_num] break return ([index_1, index_2])

Key of this solution:

1. enumerate (list_name) will walk through the index and the value;

2. put the value and index into the dictinary

3. search whether the sec_num in the dictionary (only key can be searched? )

4. if yes, the the value is the index of the sec_num

5. check whether the sec_num is actually the first number, if not , then return the index.

### Two Sum V2

Posted by pttjuniper in Uncategorized on November 17, 2017

class Solution: def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ nums_length = len (nums) index_1 = int () index_2 = int () target_list = [] for x in range (nums_length): second_num = target - nums[x] if second_num != nums[x]: if second_num in nums: index_2 = nums.index(second_num) index_1 = x break elif second_num in nums[x+1::]: index_2 = nums[x+1::].index(second_num)+x +1 index_1 = x break target_list = [index_1, index_2] return target_list

The concept in this solution:

1. have the first number,

2. second_number = target – first_number

3. check whether the second_number in list.

4. if not, then move to next number in the list

5. if yes, check whether the first number and the second number is the same: if it is the same, then make sure it is two different numbers , but starting a new list nums[x+1 ::], and then find the index in the new list. The important thing here is: for the new list starting index is not “0”, it should be “x+1”!!

6. if the second number is not the first number ,then it is easy to just identify the index.

Lesson learned:

**1. how to use the value to identify the index: list_name.index(the_value)
2. the second list starting index for nums[x+1::] is x+1 , for example nums[x+1::] [y], then the original index is (x+1) +y**

### Leetcode Two Sum – solution – V1

Posted by pttjuniper in Uncategorized on November 17, 2017

Given an array of integers, return **indices** of the two numbers such that they add up to a specific target.

You may assume that each input would have ** exactly** one solution, and you may not use the

*same*element twice.

**Example:**

Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0,1].

My first version:

nested two for loops, find the two value sum == target

My second version:

also two loops: the first loop is the same, but the second loop starts from x+1, no need to start index 0.

### how to save all the received message to a file

Posted by pttjuniper in Uncategorized on December 17, 2014

1. create a list, data []

2. define a function, to send commands, and receive the response, then append the response to the list

def srp (s, send, recv=”recv”, wait = 2):

send = s.send(“{}\n”.format (send))

time.sleep(wait)

recv = s.recv(5000000)

data.append(recv)

return recv

3. you can execute some commands and put all in the list

srp (s, “conf t”)

srp (s, “interface loop 111”)

srp (s, “ip address x.x.x.x/32”)

srp (s, “no shut”)

4. write the list to a file

f = open (“SWAN_install_uninstall_logs.txt”, “w”)

for line in data:

f.write (“{}\n”.format(line))

f.close()

### how to insert a local time to Python programmer and use it

Posted by pttjuniper in Uncategorized on December 17, 2014

imort time

t = str (time.strftime(“%Y%m%d–%H%M%S”))

then you can use the string t

for example:

filename = backup + “-” + t

### how to read a txt file and perform action based on the contents of the file

Posted by pttjuniper in Uncategorized on December 17, 2014

f = open (“device_list.txt”, “r”)

for lines in f:

ip = lines.strip (“\n”)

Then you can do something with the ip

## Recent Comments