鱼C论坛

 找回密码
 立即注册
查看: 2576|回复: 14

[已解决]一个含金量较高的题,走过路过进来看看啊

[复制链接]
发表于 2016-3-25 18:45:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 BeiLou 于 2016-3-26 13:23 编辑

    求帮我看看这个题,我写了自己的理解,希望大家帮助
    这个题的要求是输入一个参数数量(arity)和一个term;比较输入的term中的参数数量是否等于之前输入的参数数量。
举几个例子,例如输入了arity ==1 ,  term : function_one(hello), 由于term确实只有一个参数,则正确
                                     arity ==2 ,  term:  F(g( a, a ), f (a, b)) , f和g各有两个参数,F也有两个参数(即g和f)所以也正确

                                    复杂点的测试用例 11111111.png



     要求term只能包含字母,下划线,逗号,括号,空格

import sys


def is_syntactically_correct(term, arity):
        #代码
   


try:
    arity = int(input('Input an arity : '))
    if arity < 0:
        raise ValueError
except ValueError:
    print('Incorrect arity, giving up...')
    sys.exit()
print('A term should contain only letters, underscores, commas, parentheses, spaces.')
term = input('Input a term: ')
if is_syntactically_correct(term, arity):
    print('Good, the term is syntactically correct.')
else:
    print('Unfortunately, the term is syntactically incorrect.')

    我的想法是遍历term中的括号,即先找到所以左括号,当找到第一个右括号时则可以确定第一个右括号和最后一个左括号的内容,len其长度,与arity相比,以此类推。感觉像是把term分成不同的级别,从最里面的部分向外进行测量和比较。。。
    然而我琢磨了半天。。。写不出代码。。。实在是菜。。。
    值得注意的是,arity ==0,term:constant ,这样对的,但()就不对
    还有怎么控制term中若出现 [ ]中括号,{}大括号啊,数字啊之类的就全部报错。。。这个我也闹不明白

   测试用例如图
22222.png
333333333.png
最佳答案
2016-3-27 10:39:21
本帖最后由 挥舞乾坤 于 2016-3-27 10:40 编辑
  1. import re
  2. import sys
  3. ##test3 = 'ff(ff(ff(a,b,ff( aa, bb, cc )),b,ff( a,b, c)),b,ff(a,ff(a,b,c),c))'
  4. ##test2 = 'F(g( a, a ), f (a, b)) '
  5. ##test1 = 'function_one(hello)'

  6. def is_syntactically_correct(term, arity):
  7.     r = re.compile('\s*\w+\s*\((\s*\w*\s*,){%s}\s*\w*\s*\)' % str(arity - 1))
  8.    
  9.     def check(s):
  10.         if s.strip():
  11.             s1 = r.sub('',s)
  12.             if s == s1:
  13.                 return False
  14.             else:
  15.                 return check(s1)
  16.         return True
  17.    
  18.     if arity == 0 and '(' not in term and ')' not in term:
  19.         return True
  20.     else:
  21.         return check(term)

  22. if __name__ == '__main__':
  23.     try:
  24.         arity = int(input('Input an arity : '))
  25.         if arity < 0:
  26.             raise ValueError
  27.     except ValueError:
  28.         print('Incorrect arity, giving up...')
  29.         sys.exit()
  30.     print('A term should contain only letters, underscores, commas, parentheses, spaces.')
  31.     term = input('Input a term: ')
  32.     if is_syntactically_correct(term, arity):
  33.         print('Good, the term is syntactically correct.')
  34.     else:
  35.         print('Unfortunately, the term is syntactically incorrect.')
复制代码

勉强测试通过arity == 0,很牵强的办法,加了个判断而已,期待更好的办法吧
多加了这歌判断:
  1.     if arity == 0 and '(' not in term and ')' not in term:
  2.         return True
  3.     else:
  4.         return check(term)
复制代码
11111111.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-3-25 18:47:44 | 显示全部楼层
仔细想了想好像遍历括号的方法也不行啊怎么将一个复杂的元组给分清楚了,弄成一层套一层清楚的样子啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-26 10:12:10 | 显示全部楼层
@小甲鱼 @冬雪雪冬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-26 10:13:17 | 显示全部楼层
@小甲鱼 @冬雪雪冬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-26 10:57:46 | 显示全部楼层
栗子不明白,
arity ==3 ,  term:  F(g( a, a ), f (a, b))
这个为什么正确呢。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-26 13:23:17 | 显示全部楼层
wei_Y 发表于 2016-3-26 10:57
栗子不明白,
arity ==3 ,  term:  F(g( a, a ), f (a, b))
这个为什么正确呢。

打错了。。。是二,怎么@人啊。。。帮我@下小甲鱼和冬雪雪冬吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-26 14:04:34 | 显示全部楼层
BeiLou 发表于 2016-3-26 13:23
打错了。。。是二,怎么@人啊。。。帮我@下小甲鱼和冬雪雪冬吧

辣么,如果g有三个或者更多或更少参数算正确么。
@小甲鱼 @冬雪雪冬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-26 16:30:54 | 显示全部楼层
wei_Y 发表于 2016-3-26 14:04
辣么,如果g有三个或者更多或更少参数算正确么。
@小甲鱼 @冬雪雪冬

那就不对了,必须从最里到外都是3才对
你可以看看最后我贴的测试用例, 看一下就明白了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-26 18:22:28 | 显示全部楼层

回帖奖励 +8 鱼币

真是一个纠结的问题,纠结的问题用正则解决起来就更纠结(我正则超级差)
  1. import re
  2. ##test3 = 'ff(ff(ff(a,b,ff( aa, bb, cc )),b,ff( a,b, c)),b,ff(a,ff(a,b,c),c))'
  3. ##test2 = 'F(g( a, a ), f (a, b)) '
  4. ##test1 = 'function_one(hello)'

  5. def is_syntactically_correct(term, arity):
  6.     r = re.compile('\s*\w+\s*\((\s*\w*\s*,){%s}\s*\w*\s*\)' % str(arity - 1))
  7.     print(r)
  8.     def check(s):
  9.         if s.strip():
  10.             s1 = r.sub('',s)
  11.             if s == s1:
  12.                 return False
  13.             else:
  14.                 return check(s1)
  15.         return True
  16.     return check(term)

  17. if __name__ == '__main__':
  18.     try:
  19.         arity = int(input('Input an arity : '))
  20.         if arity < 0:
  21.             raise ValueError
  22.     except ValueError:
  23.         print('Incorrect arity, giving up...')
  24.         sys.exit()
  25.     print('A term should contain only letters, underscores, commas, parentheses, spaces.')
  26.     term = input('Input a term: ')
  27.     if is_syntactically_correct(term, arity):
  28.         print('Good, the term is syntactically correct.')
  29.     else:
  30.         print('Unfortunately, the term is syntactically incorrect.')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-26 21:31:22 | 显示全部楼层
试了好久没有做出来。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-27 03:44:31 | 显示全部楼层
本帖最后由 bhsx 于 2016-3-27 04:41 编辑

去掉括号前的名字.然后转成元组,然后你懂得
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-3-27 08:17:27 | 显示全部楼层
挥舞乾坤 发表于 2016-3-26 18:22
真是一个纠结的问题,纠结的问题用正则解决起来就更纠结(我正则超级差)

虽然看不大明白,但大体的功能实现了,但是怎么限制term的输入符合要求,并且arity ==0 ,输入F 或者content这样的结果也是True呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-27 10:39:21 | 显示全部楼层    本楼为最佳答案   
本帖最后由 挥舞乾坤 于 2016-3-27 10:40 编辑
  1. import re
  2. import sys
  3. ##test3 = 'ff(ff(ff(a,b,ff( aa, bb, cc )),b,ff( a,b, c)),b,ff(a,ff(a,b,c),c))'
  4. ##test2 = 'F(g( a, a ), f (a, b)) '
  5. ##test1 = 'function_one(hello)'

  6. def is_syntactically_correct(term, arity):
  7.     r = re.compile('\s*\w+\s*\((\s*\w*\s*,){%s}\s*\w*\s*\)' % str(arity - 1))
  8.    
  9.     def check(s):
  10.         if s.strip():
  11.             s1 = r.sub('',s)
  12.             if s == s1:
  13.                 return False
  14.             else:
  15.                 return check(s1)
  16.         return True
  17.    
  18.     if arity == 0 and '(' not in term and ')' not in term:
  19.         return True
  20.     else:
  21.         return check(term)

  22. if __name__ == '__main__':
  23.     try:
  24.         arity = int(input('Input an arity : '))
  25.         if arity < 0:
  26.             raise ValueError
  27.     except ValueError:
  28.         print('Incorrect arity, giving up...')
  29.         sys.exit()
  30.     print('A term should contain only letters, underscores, commas, parentheses, spaces.')
  31.     term = input('Input a term: ')
  32.     if is_syntactically_correct(term, arity):
  33.         print('Good, the term is syntactically correct.')
  34.     else:
  35.         print('Unfortunately, the term is syntactically incorrect.')
复制代码

勉强测试通过arity == 0,很牵强的办法,加了个判断而已,期待更好的办法吧
多加了这歌判断:
  1.     if arity == 0 and '(' not in term and ')' not in term:
  2.         return True
  3.     else:
  4.         return check(term)
复制代码

评分

参与人数 1荣誉 +6 鱼币 +6 收起 理由
wei_Y + 6 + 6 支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-27 10:50:33 | 显示全部楼层
BeiLou 发表于 2016-3-27 08:17
虽然看不大明白,但大体的功能实现了,但是怎么限制term的输入符合要求,并且arity ==0 ,输入F 或者cont ...

不太难懂,思路就是根据输入的arity,用正则匹配ff( , , )这样的形式,并替换成'',再用递归把替换完的字符串在1次匹配,直到结果为空,就算通过了,如果匹配不到,就False
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-3-27 13:08:29 | 显示全部楼层
  1. import sys

  2. def is_syntactically_correct(term, arity):
  3.     import re
  4.     term = re.sub(r'\s*', '', term)
  5.     condition = re.compile(r'\((.*)\)')
  6.     def find_args(term=term, arity=arity):

  7.         result = re.findall(condition ,term)
  8.         if result[0].count('(') != result[0].count(')'):
  9.             return False
  10.         len_result = len(result)
  11.         if len_result == 0 and arity == 0:
  12.             return True
  13.         elif len_result != 0 and arity == 0:
  14.             return False
  15.         if '(' not in result[0]:
  16.             lens = len(result[0].split(','))
  17.             if lens == arity:
  18.                 return True
  19.             else:
  20.                 return False

  21.         judge = [0]
  22.         trans = ''
  23.         result_arg = []
  24.         length = len(result[0])
  25.         for i in range(length):
  26.             trans += result[0][i]
  27.             if result[0][i] == ',' and '(' not in trans:
  28.                 result_arg.append(trans[:-1])
  29.                 trans = ''
  30.                 judge = [0]
  31.             if i == length-1:
  32.                 result_arg.append(trans)
  33.             if len(judge) == 0:
  34.                 result_arg.append(trans)
  35.                 trans = ''
  36.                 judge = [0]
  37.             if result[0][i] == '(' and judge[0] == 0:
  38.                 judge[0] = result[0][i]
  39.             elif result[0][i] == '(' and judge[0] != 0:
  40.                 judge.append(result[0][i])
  41.             elif result[0][i] == ')':
  42.                 judge.remove('(')
  43.         if len(result_arg) == arity:
  44.             for i in result_arg:
  45.                 if '(' in i:
  46.                     return find_args(term=i, arity=arity)
  47.                 else:
  48.                     return True
  49.         else:
  50.             return False
  51.     return find_args()


  52. try:
  53.     arity = int(input('Input an arity : '))
  54.     if arity < 0:
  55.         raise ValueError
  56. except ValueError:
  57.     print('Incorrect arity, giving up...')
  58.     sys.exit()
  59. print('A term should contain only letters, underscores, commas, parentheses, spaces.')
  60. term = input('Input a term: ')
  61. if is_syntactically_correct(term, arity):
  62.     print('Good, the term is syntactically correct.')
  63. else:
  64.     print('Unfortunately, the term is syntactically incorrect.')
复制代码


笨办法实现。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-19 02:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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