Click here to Skip to main content
15,667,829 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

// prints [("tuples", 5), ("are", 2), ("awesome", 1), ("cool", 1), ("shades", 1)]
Updated 20-Apr-17 6:03am

1 solution

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!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900