Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中从缩进字符串创建计数数组_Python_String_Algorithm - Fatal编程技术网

在python中从缩进字符串创建计数数组

在python中从缩进字符串创建计数数组,python,string,algorithm,Python,String,Algorithm,字符串str包含上述路径。 我想让游戏中的角色数量达到最大 例如: james gifts PS Gta.game crooks.movie xbox fallout.game karen brave.movie destiny.game flowers 我正在准备下个月的实习面试,我无法回避这个问题。我可以用C解决Hackerrank中的大多数字符串中硬问题。我通常用C编写代码,但我觉得像python这样的字符串操作更好 我想的方法是

字符串str包含上述路径。 我想让游戏中的角色数量达到最大 例如:

james
 gifts
   PS
    Gta.game
    crooks.movie
   xbox       
    fallout.game
karen
 brave.movie
 destiny.game
 flowers
我正在准备下个月的实习面试,我无法回避这个问题。我可以用C解决Hackerrank中的大多数字符串中硬问题。我通常用C编写代码,但我觉得像python这样的字符串操作更好

我想的方法是在每场比赛中保持一个计数,然后在每一点上与max一起“if”以保持计数的最大值。
例如:
_


但这种方法无法忽略电影的数量,如果同一个人有其他游戏。我想的另一种方法是先遍历到游戏,然后返回到人员计数,但即使这样也和以前有相同的问题。

任何关于如何解决这个问题的帮助都将不胜感激。

您完全正确,Python在字符串方面比C好得多

在算法中,我将使用一个堆栈,由一个简单的Python列表实现。 每次更新堆栈时,我都会逐行遍历文件。每行开头的空格数将告诉我是否需要将行推送到堆栈中,或者首先从堆栈中弹出一个或多个项目,然后才将行推送到堆栈中

然后,每当我进入一个游戏,我可以通过将当前在堆栈中的单词中的字符数相加来计算游戏中的字符总数。我将使用函数lstrip删除行开头的空格

count to occurrence of next space  
james(count=5)  
gifts(count=10)  
xbox(count=14)  
fallout.game(count=26)
在您给出的示例中,堆栈在每次迭代中都是这样的:

num_of_chars = sum([len(word.lstrip()) for word in stack])
num_of_chars = sum([len(word.lstrip()) for word in stack])
[]
['james']
['james',' gifts']
['james',' gifts','   PS']
['james',' gifts','   PS','    Gta.game']
['james',' gifts','   PS','    crooks.movie']
['james',' gifts','   xbox']
['james',' gifts','   xbox','    fallout.game']
['karen']
['karen',' brave.movie']
['karen',' destiny.game']
['karen',' flowers']