LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: KornLee

出道算术题[精彩回复]

[复制链接]
发表于 2004-3-23 18:59:03 | 显示全部楼层
最初由 hgkrt 发表
第一次看到女孩子如此厉害!!!

我也开始学shell programming了...

今天出去买了本Bruee Blinn写的Unix Shell Programming入门...


倩儿的代码有点冗余,我修正一下。

  1. #! /usr/bin/perl -w

  2. if(defined($_=shift @ARGV)) {
  3.     open INPUTFILE, "$_"
  4.         or die "Cannot access file $_($!)";
  5.     while (<INPUTFILE> ) {
  6.         chomp;
  7.         if(/^(\w+)\s+(\d+)$/){
  8. [color=green]                $hashdb{$1}+=$2;[/color]
  9.         }
  10.     }
  11.     foreach $field (sort keys %hashdb){
  12.         print "$field has $hashdb{$field}\n";
  13.     }
  14.     close INPUTFILE;
  15. }
  16. else{
  17.     print "No argument is supplied!\n";
  18. }
复制代码
发表于 2004-3-24 10:59:35 | 显示全部楼层

  1. #!/usr/bin/python
  2. f = open('in.txt')
  3. s = f.readline()
  4. d = {}
  5. while not s == '':
  6.     oi = s.split()
  7.     if oi[0] in d:
  8.         d[oi[0]] += int(oi[1])
  9.     else:
  10.         d[oi[0]] = int(oi[1])
  11.     s = f.readline()
  12. f.close()
  13. for x,v in d.items():
  14.     print x,v
复制代码
 楼主| 发表于 2004-3-24 11:48:02 | 显示全部楼层
呵呵,有种百花争鸣的劲头~,谢谢!
ps:虽然看不懂python :p
发表于 2004-3-24 18:43:08 | 显示全部楼层
好热闹,我也来一个
[php]
#!/usr/bin/env python
# -*- coding: GB2312 -*-
# Last modified: 2004年03月24日 星期三 18时42分40秒 [debian]
f=open('file')
a=f.readlines()
l=[[x[0], int(x[1])] for x in [x.split() for x in a]]
d={}
def ins(i,n):d=d.get(i,0)+n
[ins(x[0], x[1]) for x in l]
print '\n'.join(map(lambda x: "%s %s" %(x, d[x]), d.keys()))
[/php]
发表于 2004-3-24 20:45:33 | 显示全部楼层
今日得闲,写个纯 bash 的:

  1. [color=red]-(root@daisy)--(~/trash)-
  2. [18394 0] % cat data[/color]
  3. a 100
  4. g 200
  5. o 300
  6. g 400
  7. a 500
  8. a 600
  9. o 760
  10. [color=red]-(root@daisy)--(~/trash)-
  11. [18394 0] % cat sum.sh [/color]
  12. #/bin/bash

  13. unset -v a b c d e f g h i j k l m n o p q r s t u v w x y z

  14. while read letter number; do
  15.     eval "(($letter += $number))"
  16. done < data

  17. for letter in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
  18.     eval "[ \$$letter ] && echo sum[$letter] = \$$letter"
  19. done
  20. [color=red]-(root@daisy)--(~/trash)-
  21. [18394 0] % ./sum.sh [/color]
  22. sum[a] = 1200
  23. sum[g] = 600
  24. sum[o] = 1060
  25. [color=red]-(root@daisy)--(~/trash)-
  26. [18394 1] % [/color]
复制代码
发表于 2004-7-25 10:31:27 | 显示全部楼层
请教一下几个符号的含义:
+=, -+, *=, /=
 楼主| 发表于 2004-7-25 11:22:30 | 显示全部楼层
最初由 aaccdd 发表
请教一下几个符号的含义:
+=, -+, *=, /=

+=:递增
-=:递减
*=:递乘
/=:递除
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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