算法题:输入aaaabbbcccccc输出a4b3c6。

今日在地铁上浏览今日头条的时候看到这么个小题目,说是输出一长串字符串,输出字母串类别并且统计其出现次数,然后按照顺序将其输出来。例如输入aaaabbbcccccc,输出a4b3c6。

最近也一直在学习,所以就想着就Matlab来试了试,题目是很简单的。不是IT出身,所以可能自己的想法比较简单,但是也算是一个学习吧!

主要是为了养成记录的习惯,所以就把这个简单的东西记录下来。

直接上代码吧。

clc
clear
close
strInput='aaaabbbcccccc';
str=strInput';
strCount=tabulate(str);
letterTypeNumber=size(strCount,1);
strAppend=[];
for i=1:letterTypeNumber
    strAppend=[strAppend,strCount{i,1},num2str(strCount{i,2})];
end
disp('The final string ouyput answer is : ')        
strOutput=strAppend

最后的输出结果为:

The final string ouyput answer is : 

strOutput =

a4b3c6

看了看,最后的目的达到了。不过这里主要是使用了matlab的一个自带的统计函数tabulate。执行了下面这一句:

strCount=tabulate(str);

得到的结果为:

abc43630.769223.076946.1538

这个矩阵的第一列就是字母的类别统计,第二列是字母的出现次数统计,最后一列就是一个占比百分数。

然后再输入了一个不仅仅有字母的字符串,包括一些其他的字符。

    strInput='~~~@@$@@$%$$%&$&abcabcdefdef***^*^';

运行了一下,得到的结果为:

    The final string ouyput answer is : 

    strOutput =

    ~3@4$5%2&2a2b2c2d2e2f2*4^2

结果也还行。主要是matlab自带的函数tabulate很好用吧。

下一步打算不使用matlab自带的函数来试试。

clc
clear
close
%%
strInput='aaaabbbcccccc';
strInput=sort(strInput);
strLength=size(strInput,2);
if strLength~=0
    temp=1;
else
    disp('The Input is null !')
end
 strCountSum=0;  
for i=1:strLength
       if i==strLength
           strCount(temp)=strLength-strCountSum;
           strType(temp)=strInput(strLength)
           break;
       elseif strInput(1,i)~=strInput(1,i+1)
           if temp-1==0
                strCount(temp)=i;
           else
               strCount(temp)=i-strCountSum;
           end
           strType(temp)=strInput(1,i)
           strCountSum=strCountSum+strCount(temp);
           temp=temp+1;
       end
end

strAppend=[];
for i=1:temp
    strAppend=[strAppend,strType(1,i),num2str(strCount(1,i))];
end
disp('The final string ouyput answer is : ')        
strOutput=strAppend

输出结果为:

The final string ouyput answer is : 

strOutput =

a4b3c6

发现,结果也是对的。

然后再输入了一个不仅仅有字母的字符串,包括一些其他的字符。

strInput='~~~@@$@@$%$$%&$&abcabcdefdef***^*^';  

输出的结果为:

The final string ouyput answer is : 

strOutput =

$5%2&2*4@4^2a2b2c2d2e2f2~3

结果也是对的,但是和上面的结果稍微有一点排序上的差别。这个目前还没弄清楚这个tabulate对于字符的排序和sort函数对于字符的排序有什么区别。

Python字典实现该算法题

最近在学习Python数据结构之字典,突然发现,这个数据结构还是相当好用的,再联想到这个算法题,决定试一试。

话不多说,先上代码吧

def string_count_append(string):
    d = {}
    for i in string:
        # 相当于创建字典,当没有key‘i’时,返回该key对应value=0
        d[i] = d.get(i, 0) + 1
    # 最终得到一个以出现字符为key,字符出现次数为value的字典

    finalString = ''
    # d.items() 返回字典的key和value且是成对出现,为元组类型

    for i in d.items():
        #字符连接
        #'%d'%i[1]实现数字转换为字符
        temp = i[0] + '%d' % i[1]
        finalString += temp
    return finalString
if __name__=="__main__":
    str = 'aaaabbcccccc'
    print()
    print('The string before counting and appending is:\n')
    print(str)
    print()
    print('The string after counting and appending is:\n')
    print(string_count_append(str))
    print()

得到的结果为:

这里写图片描述

可以看出来,使用Python以及字典这种数据结构,很快就得到结果了。代码简单明了。
暂时没有发现什么bug。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值