如何使用元组计算子字符串(单词)出现在字符串中的次数 [英] How to count the number of times a substring(word) has appeared in a string using tuples

查看:207
本文介绍了如何使用元组计算子字符串(单词)出现在字符串中的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是问题 -



您将获得存储在变量字符串中的字符串数组。创建一个名为countsStrings的新数组,其中包含type(String,Int)的值。每个元组都包含一个来自strings数组的字符串,后跟一个整数,表示它在strings数组中出现的次数。每个字符串应该只在countsStrings数组中出现一次。



我尝试过:



我还没有解决问题,因为它已经解决了。我只是想了解它。我可以理解的部分内容和我无法理解的部分内容。请你们任何一位先生帮助我理解我无法理解的部分。解决了: -



var a = [tuples,are,awesome,tuples,are,cool,
元组,元组,元组,阴影]



var y:[(String,Int)] = [] /// * [(String,Int)]:变量的类型,在这种情况下类型是Array of(String,Int),编译器将允许您只插入类型为(String,Int)的对象阵列。 []:是默认的空数组。



for z in {// here z表示子串,a表示字符串

var x = false



for i in 0 ..< y.count {// i = 0,i< y.count(因为数组以0开头,计数从一开始

如果(y [i] .0 == z){//无法理解

y [i] .1 + = 1 //无法理解

x = true //无法理解

}

}

如果x == false {//无法理解

让tuple =(z,2)//无法理解

y.append((z,1))//无法理解

}

}

打印(y)







// print [(tuples,5),(are,2),(awesome,1),(cool,1),(shades,1)]

this is the problem -

You are given an array of strings stored in the variable strings. Create a new array named countedStrings containing values of type (String,Int). Each tuple contains a string from the strings array followed by an integer indicating how many times it appears in the strings array. Each string should only appear once in the countedStrings array.

What I have tried:

I have not solved the problem as It is already solved. I am just trying to understand it. Some part of it i could understand and some parts I could not. Please any of you gentleman help me to understand the parts I could not understand. solved:-

var a = ["tuples", "are", "awesome", "tuples", "are", "cool",
"tuples", "tuples", "tuples", "shades"]

var y: [(String,Int)] = []///*[(String,Int)]: the type of your variable, in this case the type is Array of (String,Int), the compiler will allow you to insert only object of type (String,Int) in your Array. []: is default empty array.

for z in a{ // here z represents substring, a represents string
var x = false

for i in 0..<y.count {// i=0, i<y.count(because array starts with 0 and count starts with one
if (y[i].0 == z) {// could not understand
y[i].1 += 1// could not understand
x = true// could not understand
}
}
if x == false {// could not understand
let tuple = (z,2)// could not understand
y.append((z,1))// could not understand
}
}
print(y)



// prints [("tuples", 5), ("are", 2), ("awesome", 1), ("cool", 1), ("shades", 1)]

推荐答案

如果你在互联网上找到代码并且你不明白,这就是你的问题 - 特别是在你的家庭作业时。



而不是找到代码并希望抛弃它,阅读问题,编写自己的代码。这可能看起来更难,但这是你正确学习的唯一方法 - 如果你不能自己解决这样的简单问题,那么当较难的人开始出现时,你将完全迷失。



说真的:亲自试一试。当你有工作代码时,你可能会看到它并告诉我们它是如何工作的!
If you find code on the internet and you don;t understand it, that's your problem - particularly when it's your homework.

Instead of finding code and hoping, throw that away, read the question, and write your own code. It may seem harder, but it's the only way you are going to learn properly - and if you can't solve simple problems like this yourself, then you are going to be completely lost when the harder ones start to crop up.

Seriously: give it a try yourself. By the time you have working code, you will probably be able to look at that and tell us how it works!


这篇关于如何使用元组计算子字符串(单词)出现在字符串中的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆