Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782292
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2018-04-09 10:45:11

继续昨天的Array problem:

4.

#check two strings are anagrams:

#eg: "public relations" <--> "crap built on lies"
这个anagram_checker 见过无数次了

点击(此处)折叠或打开

  1. def anagram_checker(s1, s2):
  2.     s1 = s1.replace(' ','').lower()
  3.     s2 = s2.replace(' ','').lower()
  4.     
  5.     return sorted(s1) == sorted(s2)


  6. def anagram_checker_2(s1, s2):
  7.     s1 = s1.replace(' ','').lower()
  8.     s2 = s2.replace(' ','').lower()
  9.     
  10.     if len(s1) != len(s2):
  11.         return False
  12.     
  13.     count = {}
  14.     
  15.     for letter in s1:
  16.         if letter in count:
  17.             count[letter] +=1
  18.         else:
  19.             count[letter] = 1
  20.     
  21.     for letter in s2:
  22.         if letter in count:
  23.             count[letter] -= 1
  24.         else:
  25.             count[letter]= 1
  26.     
  27.     for k in count:
  28.         if count[k] != 0:
  29.             return False
  30.     
  31.     return True

  32. def anagram_tester():
  33.     s1="public relations"
  34.     s2="crap built on lies"
  35.     print(anagram_checker(s1,s2))
  36.     print(anagram_checker_2(s1,s2))
  37.     
  38. anagram_tester()


5.

#given an integer array, output all the unique pairs that sum up to a specific value k

#eg: pair_sum([1,3,2,2] , 4)

#get : ((1,3),(2,2))



点击(此处)折叠或打开

  1. def pair_sum(array, k):
  2.     
  3.     if len(array) < 2:
  4.         return False
  5.     
  6.     #sets for tracking
  7.     seen = set()
  8.     output = set()
  9.     
  10.     for num in array:
  11.         target = k - num
  12.         
  13.         if target not in seen:
  14.             seen.add(num)
  15.     
  16.         else:
  17.             output.add((min(num,target), max(num,target)))
  18.                 
  19.     #return len(output)
  20.     return '\n'.join(map(str,list(output)))

  21.  
  22. def pair_sum_test():
  23.     print(pair_sum([1,3,2,2],4))

  24. pair_sum_test()

6.

#find the missing elem:

'''given an array of non-negative Integers, A second array is formed by shuffle the elements

of the first array and deleting a random element. Given the two arrays, find which

elements is missing in the second array.

'''

这个XOR(异或)的用法让我想起来以前C语言不用临时变量(指针)交换两个变量的值:
c语言里面会这样:
a=a^b
b=a^b
a=a^b

点击(此处)折叠或打开

  1. def find_missing_elem(arr1, arr2):
  2.     sortarr1=sorted(arr1)
  3.     sortarr2=sorted(arr2)
  4.     
  5.     for num1, num2 in zip(sortarr1, sortarr2):
  6.         if num1 != num2:
  7.             return num1
  8.     
  9.     return sortarr1[-1]


  10. import collections

  11. def find_missing_elem2(arr1, arr2):
  12.     d = collections.defaultdict(int)
  13.     
  14.     for num in arr2:
  15.         d[num] += 1
  16.     
  17.     for num in arr1:
  18.         if d[num] == 0:
  19.             return num
  20.         else:
  21.             d[num] -= 1
  22.         

  23. #only applies when arr1 and arr2 are small, also they are integers arrays
  24. def find_missing_elem3(arr1, arr2):
  25.     return sum(arr1) - sum(arr2)
  26.     


  27. #utilize XOR
  28. def find_missing_elem4(arr1, arr2):
  29.     
  30.     result = 0
  31.     
  32.     for num in (arr1 + arr2):
  33.         result ^= num
  34.         #print(result)
  35.     
  36.     return result



  37. def find_missing_elem_test():
  38.     print(find_missing_elem4((1,2,3,4),(4,3,2)))
  39.     print(find_missing_elem((1,2,3,4),(4,3,2)))
  40.     print(find_missing_elem2((1,2,3,4),(4,3,2)))
  41.     print(find_missing_elem3((1,2,3,4),(4,3,2)))

  42. find_missing_elem_test()


阅读(756) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~