word ='great'for char in word[:-1]:print(char, end =',')print(word[-1])
g,r,e,a,t
enumerate is a function that may be new to you. It is used in situations where you need both the character and the index of a string that you are iterating over.
word ='great'for index, char inenumerate(word):if index !=len(word) -1:print(char, end =',')else:print(char)
g,r,e,a,t
word ='great'index =0while index <len(word) -1:print(word[index], end =',') index +=1print(word[-1])
g,r,e,a,t
Find number of characters in a string, the hard way
word ='sound'length =0for char in word: length +=1print(length)
5
Find if a character is present in a string, the hard way
sentence ='this, is, not, formatted, properly'out =''# empty stringfor char in sentence:if char !=',': out = out + charprint(out)
this is not formatted properly
sentence ='this, is, not, formatted, properly'out =''# empty stringfor char in sentence:if char ==',':continue out = out + charprint(out)
this is not formatted properly
sentence ='this, is, not, formatted, properly'out =''# empty stringindex =0while index <len(sentence): char = sentence[index]if char !=',': out = out + char index +=1print(out)
n =10count =0for f inrange(1, n +1):if n % f ==0: count +=1print(count)
4
n =10count =0f =1while f <= n:if n % f ==0: count +=1 f +=1print(count)
4
Determine if a number is perfect
A positive integer \(n\) is termed perfect if the sum of all its factors excluding the number itself is equal to \(n\). For example, \(6\) is a perfect number because \(1 + 2 + 3 = 6\). Likewise, \(28\) is a perfect number because \(1 + 2 + 4 + 7 + 14 = 28\).
n =int(input())count =0for f inrange(1, n +1):if n % f ==0: count +=1if count ==2:print('PRIME')else:print('NOTPRIME')
n =int(input())# optimistic startis_prime =Truefor f inrange(1, n +1):if (n % f ==0) and (f !=1) and (f != n): is_prime =Falsebreakif is_prime:print('PRIME')else:print('NOTPRIME')
Though the following code will not work for \(n = 1\), the question states that \(n > 1\). We have made use of this fact. This code is a slight improvement over method-2.
n =int(input())# optimistic startis_prime =Truefor f inrange(2, n):if n % f ==0: is_prime =Falsebreakif is_prime:print('PRIME')else:print('NOTPRIME')
Print all pairs of ordered positive integers that sum to \(s\)
In this question, we assume that the order of elements in the pair matters. For example, if \(s = 100\), then \((10, 90)\) and \((90, 10)\) are treated as two different pairs.
We loop from \(1\) to \(s - 1\). \(s\) is excluded as we are only interested in positive integers that sum to \(s\).
s =int(input())for x inrange(1, s):for y inrange(1, s):if x + y == s:print(x, y)print(s)
Any pair that sums to \(s\) can be represented as \((x, s - x)\). Using this idea, we can dispense with the second loop. This method is therefore an improvement over method-1.
s =int(input())for x inrange(1, s):print(x, s - x)
These two methods illustrate a common observation in problem solving. The first solution is mostly going to be raw and inefficient. The art of programming is to refine this solution so as to make it more efficient.
Find all Pythagorean triplets \((x, y, z)\) with \(x < y < z < n\)
The purpose of this how-to is to introduce you to triply nested loops.
n =int(input())for x inrange(1, n):for y inrange(1, n):for z inrange(1, n):if x < y < z:if (x **2+ y **2== z **2):print(x, y, z)
Since \(x < y < z\), it is sufficient if we have the following ranges for the three loops:
\(1 \leqslant x < n\)
\(x < y < n\)
\(y < z < n\)
This considerably reduces the number of iterations overall.
n =int(input())for x inrange(1, n):for y inrange(x +1, n):for z inrange(y +1, n):if (x **2+ y **2== z **2):print(x, y, z)
There is some more wasteful computation happening. Once we know that \(x^{2} + y^{2} \geqslant n^2\), there is no point in looking for \(z\). Therefore, we have the following code:
n =int(input())for x inrange(1, n):for y inrange(x +1, n): lhs = x **2+ y **2if (lhs >= n **2):breakfor z inrange(y +1, n):if lhs == z **2:print(x, y, z)
Some parts of this code may look awkward. In line-5, we are computing the lhs once for each value of \(y\). This is to avoid computing \(x^{2} + y^{2}\) each time in the innermost loop.