找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

楼主: roommate
收起左侧

[Goldman Sachs] Goldman Sachs新鲜电面

[复制链接]

1206

主题

184

精华

3720

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3720
发表于 2-8-2017 11:03 PM | 显示全部楼层
8 F- P' }: c0 x% O1 r. `6 F) M
0 s/ D% S2 F# i7 ?& m2 L * ?. v m( Q, S0 e- e3 R
2
6 |/ Z9 x* t) n% I$ g& u% O
, ^% L6 Z2 p$ n% Z" ^# Y! @9 j9 f 0 r9 I! |) t& Z, h
of 2 votes
5 D& x- _; c, z7 h, t: k, x5 M
) {. Z9 i0 R8 L4 t
5 W5 v' r* I" c, ~* y$ J. B ; N3 H! G1 z" o4 X/ `

Almost.( | W6 N! D% x4 U
5 }# b; Q4 V; m7 N' h6 S- {
Let's define ascii values: . y* H" C$ w8 |5 x. z
) P( N m7 l: x, [7 Q# l
A= 650 g Y' k+ N7 M0 U
B = 66/ [9 e! t1 @1 f+ n
C = 67 k% \$ ~: p6 X% ?8 z
D = 68 ' U1 |; L( u. b' e4 h( n) l3 D
E = 69 / A1 x) E/ ]) n; J4 t2 u
... etc. 7 a2 m" y, f1 U7 V
! q5 U/ }* q" ]9 S3 s
If we have two strings "BE" and "DC", they both sum to the same value, but they have different characters. - w/ N& Q& k8 ]/ D5 ~3 @: {: [8 z
. N1 Y" }1 r L5 o# |% P4 h' J
I think instead of transforming based on the sum of characters, when you iterate over the list, the hash code should be the hashcode produced when the particular string is lexicographically ordered. ) H) f9 x6 x" C- n: ^( s4 I
/ G9 u1 c+ u" @
so if you have the following strings: % g0 s- D+ y" c5 F3 q8 k$ } x$ D7 b8 |
3 m5 c8 {, f3 O" b0 ?, {5 Y8 p0 h
"BAC", "CAB"8 n h0 ~( I7 d2 a* C \
( x& N9 W( ?6 m7 Z0 V- X' r
we would first find the lexicographically sorted version of the string , (for both this is "ABC") and in the hash location for "ABC", we would add both of them

1228

主题

188

精华

3782

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3782
发表于 2-8-2017 11:03 PM | 显示全部楼层
; K2 |4 ]: d5 X. x
; @ t3 D% ?. j ) `9 ?3 f S; i [
1
3 h7 D w1 \% b
( F& E( J) o+ A& r 9 K. W4 _, Q3 z& a! Q- e. P4 A
of 1 vote
@# N# x# D) ^, c! \4 x
" q5 B3 U8 I8 Z8 z9 t+ B- R
$ w5 G" i% S( E! s1 b " A4 N" l4 k8 G) X9 d9 b

You are right, definitely missed that detail, your solution would cover that issue.

1183

主题

187

精华

3729

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3729
发表于 2-8-2017 11:03 PM | 显示全部楼层

def anagramQuestion(list1, input):; G# X1 J, u, [* z7 O, L
	dict1 = {}
9 x' l5 c2 b8 B, _* W	for x in range(0,len(list1)):
/ Y( |2 V; E+ M( k! ]		key = ''.join(sorted(list1[x]))
7 X& U. P" k. `+ p		if key in dict1:6 ?5 f! `1 u& S2 [
			dict1[key].append(list1[x])' e( Q1 o: r+ H. U1 h$ @, r, D! }" C  j
		else:
  b6 ]8 [% x/ M# B/ b			dict1[key] = [list1[x]]; i, B# V  [, r! ]' {4 @2 g8 L
	result = []# ~3 [! W4 o& H( M
	for x in dict1:+ {. X3 J! L. x/ o
		inputsorted = ''.join(sorted(input)); ~' I1 V' @. B  O' G* J( S0 s
		if x == inputsorted:
) ?( F) X0 u( E9 u			dict1[x].remove(input)9 s, N( f8 q$ i1 c2 O# P
			result.append(dict1[x])+ A+ u  ~0 o, ]7 @4 ]

, F8 N' p, P7 d) s	return result

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表