卖房子上哪个网站最好卖湖南网络营销
news/
2025/9/23 21:08:49/
文章来源:
卖房子上哪个网站最好卖,湖南网络营销,做网站的软件下载,网站做优化有什么好处今日在地铁上浏览今日头条的时候看到这么个小题目#xff0c;说是输出一长串字符串#xff0c;输出字母串类别并且统计其出现次数#xff0c;然后按照顺序将其输出来。例如输入aaaabbbcccccc#xff0c;输出a4b3c6。
最近也一直在学习#xff0c;所以就想着就Matlab来试了…今日在地铁上浏览今日头条的时候看到这么个小题目说是输出一长串字符串输出字母串类别并且统计其出现次数然后按照顺序将其输出来。例如输入aaaabbbcccccc输出a4b3c6。
最近也一直在学习所以就想着就Matlab来试了试题目是很简单的。不是IT出身所以可能自己的想法比较简单但是也算是一个学习吧
主要是为了养成记录的习惯所以就把这个简单的东西记录下来。
直接上代码吧。
clc
clear
close
strInputaaaabbbcccccc;
strstrInput;
strCounttabulate(str);
letterTypeNumbersize(strCount,1);
strAppend[];
for i1:letterTypeNumberstrAppend[strAppend,strCount{i,1},num2str(strCount{i,2})];
end
disp(The final string ouyput answer is : )
strOutputstrAppend
最后的输出结果为
The final string ouyput answer is : strOutput a4b3c6看了看最后的目的达到了。不过这里主要是使用了matlab的一个自带的统计函数tabulate。执行了下面这一句
strCounttabulate(str);得到的结果为 abc43630.769223.076946.1538
\begin{matrix}a 第二列是字母的出现次数统计最后一列就是一个占比百分数。
然后再输入了一个不仅仅有字母的字符串包括一些其他的字符。 strInput~~~$$%$$%$abcabcdefdef***^*^;运行了一下得到的结果为 The final string ouyput answer is : strOutput ~34$5%22a2b2c2d2e2f2*4^2结果也还行。主要是matlab自带的函数tabulate很好用吧。
下一步打算不使用matlab自带的函数来试试。
clc
clear
close
%%
strInputaaaabbbcccccc;
strInputsort(strInput);
strLengthsize(strInput,2);
if strLength~0temp1;
elsedisp(The Input is null !)
endstrCountSum0;
for i1:strLengthif istrLengthstrCount(temp)strLength-strCountSum;strType(temp)strInput(strLength)break;elseif strInput(1,i)~strInput(1,i1)if temp-10strCount(temp)i;elsestrCount(temp)i-strCountSum;endstrType(temp)strInput(1,i)strCountSumstrCountSumstrCount(temp);temptemp1;end
endstrAppend[];
for i1:tempstrAppend[strAppend,strType(1,i),num2str(strCount(1,i))];
end
disp(The final string ouyput answer is : )
strOutputstrAppend
输出结果为
The final string ouyput answer is : strOutput a4b3c6发现结果也是对的。
然后再输入了一个不仅仅有字母的字符串包括一些其他的字符。
strInput~~~$$%$$%$abcabcdefdef***^*^; 输出的结果为
The final string ouyput answer is : strOutput $5%22*44^2a2b2c2d2e2f2~3结果也是对的但是和上面的结果稍微有一点排序上的差别。这个目前还没弄清楚这个tabulate对于字符的排序和sort函数对于字符的排序有什么区别。
Python字典实现该算法题
最近在学习Python数据结构之字典突然发现这个数据结构还是相当好用的再联想到这个算法题决定试一试。
话不多说先上代码吧
def string_count_append(string):d {}for i in string:# 相当于创建字典当没有key‘i’时返回该key对应value0d[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 tempreturn finalString
if __name____main__:str aaaabbccccccprint()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。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/913889.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!