Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8115748

Browse files
.txt doc contain question
1 parent 4275b79 commit 8115748

13 files changed

+286
-0
lines changed

‎Sets/Introduction to Sets.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def average(array):
2+
se = set(array)
3+
length = len(se)
4+
return(sum(se)/length)
5+
6+
if __name__ == '__main__':
7+
n = int(input())
8+
arr = list(map(int, input().split()))
9+
result = average(arr)
10+
print(result)

‎Sets/Introduction to Sets.txt‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
A set is an unordered collection of elements without duplicate entries.
2+
When printed, iterated or converted into a sequence, its elements will appear in an arbitrary order.
3+
4+
Example
5+
6+
>>> print set()
7+
set([])
8+
9+
>>> print set('HackerRank')
10+
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
11+
12+
>>> print set([1,2,1,2,3,4,5,6,0,9,12,22,3])
13+
set([0, 1, 2, 3, 4, 5, 6, 9, 12, 22])
14+
15+
>>> print set((1,2,3,4,5,5))
16+
set([1, 2, 3, 4, 5])
17+
18+
>>> print set(set(['H','a','c','k','e','r','r','a','n','k']))
19+
set(['a', 'c', 'r', 'e', 'H', 'k', 'n'])
20+
21+
>>> print set({'Hacker' : 'DOSHI', 'Rank' : 616 })
22+
set(['Hacker', 'Rank'])
23+
24+
>>> print set(enumerate(['H','a','c','k','e','r','r','a','n','k']))
25+
set([(6, 'r'), (7, 'a'), (3, 'k'), (4, 'e'), (5, 'r'), (9, 'k'), (2, 'c'), (0, 'H'), (1, 'a'), (8, 'n')])
26+
Basically, sets are used for membership testing and eliminating duplicate entries.
27+
28+
Task
29+
30+
Now, let's use our knowledge of sets and help Mickey.
31+
32+
Ms. Gabriel Williams is a botany professor at District College. One day, she asked her student Mickey to compute the average of all the plants with distinct heights in her greenhouse.
33+
34+
Formula used:
35+
36+
Input Format
37+
38+
The first line contains the integer, , the total number of plants.
39+
The second line contains the space separated heights of the plants.
40+
41+
Constraints
42+
43+
44+
Output Format
45+
46+
Output the average height value on a single line.
47+
48+
Sample Input
49+
50+
10
51+
161 182 161 154 176 170 167 171 170 174
52+
Sample Output
53+
54+
169.375
55+
Explanation
56+
57+
Here, set is the set containing the distinct heights. Using the sum() and len() functions, we can compute the average.
58+

‎String's/Find a string.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def count_substring(string, sub_string):
2+
c=0
3+
le = len(sub_string)
4+
for i in range(len(string)):
5+
if string[i:le+i] == sub_string:
6+
c = c + 1
7+
if le+i == len(string):
8+
break
9+
return(c)
10+
11+
if __name__ == '__main__':
12+
string = input().strip()
13+
sub_string = input().strip()
14+
15+
count = count_substring(string, sub_string)
16+
print(count)

‎String's/Find a string.txt‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
2+
3+
NOTE: String letters are case-sensitive.
4+
5+
Input Format
6+
7+
The first line of input contains the original string. The next line contains the substring.
8+
9+
Constraints
10+
11+
12+
Each character in the string is an ascii character.
13+
14+
Output Format
15+
16+
Output the integer number indicating the total number of occurrences of the substring in the original string.
17+
18+
Sample Input
19+
20+
ABCDCDC
21+
CDC
22+
Sample Output
23+
24+
2
25+
Concept
26+
27+
Some string processing examples, such as these, might be useful.
28+
There are a couple of new concepts:
29+
In Python, the length of a string is found by the function len(s), where is the string.
30+
To traverse through the length of a string, use a for loop:
31+
32+
for i in range(0, len(s)):
33+
print (s[i])
34+
A range function is used to loop over some length:
35+
36+
range (0, 5)
37+
Here, the range loops over to . is excluded.

‎String's/Mutations.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def mutate_string(string, position, character):
2+
st = list(string)
3+
st[position] = character
4+
return("".join(st))
5+
6+
7+
if __name__ == '__main__':
8+
s = input()
9+
i, c = input().split()
10+
s_new = mutate_string(s, int(i), c)
11+
print(s_new)

‎String's/Mutations.txt‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
We have seen that lists are mutable (they can be changed), and tuples are immutable (they cannot be changed).
2+
3+
Let's try to understand this with an example.
4+
5+
You are given an immutable string, and you want to make changes to it.
6+
7+
Example
8+
9+
>>> string = "abracadabra"
10+
You can access an index by:
11+
12+
>>> print string[5]
13+
a
14+
What if you would like to assign a value?
15+
16+
>>> string[5] = 'k'
17+
Traceback (most recent call last):
18+
File "<stdin>", line 1, in <module>
19+
TypeError: 'str' object does not support item assignment
20+
How would you approach this?
21+
22+
One solution is to convert the string to a list and then change the value.
23+
Example
24+
25+
>>> string = "abracadabra"
26+
>>> l = list(string)
27+
>>> l[5] = 'k'
28+
>>> string = ''.join(l)
29+
>>> print string
30+
abrackdabra
31+
Another approach is to slice the string and join it back.
32+
Example
33+
34+
>>> string = string[:5] + "k" + string[6:]
35+
>>> print string
36+
abrackdabra
37+
Task
38+
Read a given string, change the character at a given index and then print the modified string.
39+
40+
Input Format
41+
The first line contains a string, .
42+
The next line contains an integer , denoting the index location and a character separated by a space.
43+
44+
Output Format
45+
Using any of the methods explained above, replace the character at index with character .
46+
47+
Sample Input
48+
49+
abracadabra
50+
5 k
51+
Sample Output
52+
53+
abrackdabra

‎String's/New Text Document.txt‎

Whitespace-only changes.

‎String's/String Split and Join.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def split_and_join(line):
2+
line = line.split()
3+
return("-".join(line))
4+
5+
if __name__ == '__main__':
6+
line = input()
7+
result = split_and_join(line)
8+
print(result)

‎String's/String Split and Join.txt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
In Python, a string can be split on a delimiter.
2+
3+
Example:
4+
5+
>>> a = "this is a string"
6+
>>> a = a.split(" ") # a is converted to a list of strings.
7+
>>> print a
8+
['this', 'is', 'a', 'string']
9+
Joining a string is simple:
10+
11+
>>> a = "-".join(a)
12+
>>> print a
13+
this-is-a-string
14+
Task
15+
You are given a string. Split the string on a " " (space) delimiter and join using a - hyphen.
16+
17+
Input Format
18+
The first line contains a string consisting of space separated words.
19+
20+
Output Format
21+
Print the formatted string as explained above.
22+
23+
Sample Input
24+
25+
this is a string
26+
Sample Output
27+
28+
this-is-a-string

‎String's/What's Your Name.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def print_full_name(a, b):
2+
print("Hello",end=" ")
3+
print(a,end=" ")
4+
print(b,end="")
5+
print("! You just delved into python.")
6+
if __name__ == '__main__':
7+
first_name = input()
8+
last_name = input()
9+
print_full_name(first_name, last_name)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /