r*******y 发帖数: 1081 | 1 【 以下文字转载自 Java 讨论区 】
发信人: romancity (山顶一枝草), 信区: Java
标 题: javascript
发信站: BBS 未名空间站 (Mon Oct 3 13:28:42 2011, 美东)
I have a file 1.txt
//1.txt
A B
B C
D E
E F
G H
I J
two letters in each line are in same group. So for the file above
A B C is in one group
D E F is in second group
G H is in third group
I J is in the fourth group
I need to use javascript to get the output below:
There are 4 groups:
group 1: A, B, C
group 2: D, E, F
group 3: G, H
group 4: I, J
How to write the javascript code ? Thanks a lot |
X****r 发帖数: 3557 | 2 You sure you want to use JavaScript? Which environment you're
running JS in? Say, how do you read a file?
【在 r*******y 的大作中提到】 : 【 以下文字转载自 Java 讨论区 】 : 发信人: romancity (山顶一枝草), 信区: Java : 标 题: javascript : 发信站: BBS 未名空间站 (Mon Oct 3 13:28:42 2011, 美东) : I have a file 1.txt : //1.txt : A B : B C : D E : E F
|
r*******y 发帖数: 1081 | 3 thanks a lot. I read the homework again and find that I can use any language.
I may try linux shell script
【在 X****r 的大作中提到】 : You sure you want to use JavaScript? Which environment you're : running JS in? Say, how do you read a file?
|
i*****o 发帖数: 1714 | 4 用node.js,这个东西可以替代很多perl,bash在做的事。现在要自己下载,估计以后
的linux都会包括吧。
【在 X****r 的大作中提到】 : You sure you want to use JavaScript? Which environment you're : running JS in? Say, how do you read a file?
|
a***y 发帖数: 2803 | 5 用perl挺好的,读入一行后,用split(' ',$myline)把行分成2份放进一个array,然后比
较上一个array的第二个元素和这一个array的第一个元素是否相等,if yes,合并成一个
array.
【在 r*******y 的大作中提到】 : thanks a lot. I read the homework again and find that I can use any language. : I may try linux shell script
|
r*******y 发帖数: 1081 | 6 A B
C B
in this case A, B, C are also in same group
I think the time complexity is a big problem here.
I want to use c++ now
【在 a***y 的大作中提到】 : 用perl挺好的,读入一行后,用split(' ',$myline)把行分成2份放进一个array,然后比 : 较上一个array的第二个元素和这一个array的第一个元素是否相等,if yes,合并成一个 : array.
|
t****t 发帖数: 6806 | 7 http://en.wikipedia.org/wiki/Disjoint-set_data_structure
i dunno why you focus on programming language while dealing with this
typical algorithm problem
【在 r*******y 的大作中提到】 : A B : C B : in this case A, B, C are also in same group : I think the time complexity is a big problem here. : I want to use c++ now
|
i*****o 发帖数: 1714 | 8 There are only 27 letters, where is the time complexity? All you need is to
start eith 27 empty pointers. The time for this operation is most likely O(n
).
★ 发自iPhone App: ChineseWeb - 中文网站浏览器
【在 r*******y 的大作中提到】 : A B : C B : in this case A, B, C are also in same group : I think the time complexity is a big problem here. : I want to use c++ now
|
r*******y 发帖数: 1081 | 9 thanks a lot.
The problem is that it is not just about letters. A, B, C stands names
and the data may be huge
to
(n
【在 i*****o 的大作中提到】 : There are only 27 letters, where is the time complexity? All you need is to : start eith 27 empty pointers. The time for this operation is most likely O(n : ). : : ★ 发自iPhone App: ChineseWeb - 中文网站浏览器
|
t****t 发帖数: 6806 | 10 i thought the number of letters is 26, no?...
to
(n
【在 i*****o 的大作中提到】 : There are only 27 letters, where is the time complexity? All you need is to : start eith 27 empty pointers. The time for this operation is most likely O(n : ). : : ★ 发自iPhone App: ChineseWeb - 中文网站浏览器
|
i*****o 发帖数: 1714 | 11 and I always thought it's 27!
And I proved that using gdb:
(gdb) p 'z'-'a'
$2 = 25
Now we know gdb is the best tool ever! I found no other way in the world to
give me the number of alphabets in just one line of code.
【在 t****t 的大作中提到】 : i thought the number of letters is 26, no?... : : to : (n
|
i*****o 发帖数: 1714 | 12 Then you are right, javascript is not suitable for this.
【在 r*******y 的大作中提到】 : thanks a lot. : The problem is that it is not just about letters. A, B, C stands names : and the data may be huge : : to : (n
|
t****t 发帖数: 6806 | 13 didn't it print 25? so it's really 25?
to
【在 i*****o 的大作中提到】 : and I always thought it's 27! : And I proved that using gdb: : (gdb) p 'z'-'a' : $2 = 25 : Now we know gdb is the best tool ever! I found no other way in the world to : give me the number of alphabets in just one line of code.
|
i*****o 发帖数: 1714 | 14 haha, you cant trick me on this one, its really 26:)
★ 发自iPhone App: ChineseWeb - 中文网站浏览器
【在 t****t 的大作中提到】 : didn't it print 25? so it's really 25? : : to
|