Skip to content

Is narcissistic #6052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
00c22a3
Added censor function
meg-1 May 16, 2021
4194344
Added censor code
meg-1 May 17, 2021
e7d4102
Added comments to the code
meg-1 May 17, 2021
93bd4ac
modified censor function
meg-1 May 22, 2021
b714278
added decrypt function
meg-1 Aug 1, 2021
c879c7f
added cypher and decypher functions, deleted censor and decrypt funct…
meg-1 Aug 4, 2021
e60dd6f
Deleted decrypt.py
meg-1 Aug 4, 2021
b3e0cb8
Deleted censor.py
meg-1 Aug 4, 2021
e14b9ef
Merge branch 'TheAlgorithms:master' into master
meg-1 Aug 4, 2021
947f7d8
edited the crypt and decrypt files
meg-1 Aug 4, 2021
daf21ad
Update cypher_txt.py
meg-1 Aug 4, 2021
d3d8b29
Remove the endline in cypher.py
meg-1 Aug 4, 2021
752c197
Removed the print at the end of decypher.py
meg-1 Aug 4, 2021
9f44028
comitting changes
meg-1 Aug 18, 2021
f72431b
added 4 new algorithms
meg-1 Aug 18, 2021
4c891f4
added tests to the four files
meg-1 Aug 25, 2021
c600b10
added type hints for the function variables
meg-1 Aug 25, 2021
4286e40
Merge branch 'TheAlgorithms:master' into master
meg-1 Dec 11, 2021
7a909e2
Deleted decode message
meg-1 Dec 25, 2021
f5b5411
Deleted code message
meg-1 Dec 25, 2021
bc8388c
Welford average algorithm
meg-1 Jan 29, 2022
9be479e
added average welford algorithm
meg-1 Feb 9, 2022
f3fb7e6
is_narcissistic added
meg-1 Mar 19, 2022
d00d135
added a descriptive name
meg-1 Mar 23, 2022
0fd2747
decrypt.py
meg-1 Apr 6, 2022
71e1492
Merge pull request #1 from meg-1/decrypt
meg-1 Apr 6, 2022
59a3c04
deleted decrypt.py
meg-1 Apr 9, 2022
3060e3e
deleted censor.py [ is in the wrong pr ]
meg-1 Apr 16, 2022
4d0856c
deleted average_welford [ also in the wrong pr ]
meg-1 Apr 16, 2022
9ad611f
fixed errors with tests
meg-1 Apr 16, 2022
1c3c89c
added indendation
meg-1 Apr 20, 2022
0ea36ea
removed indent
meg-1 Apr 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions maths/average_welford.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def average_welford(values: list) -> float:
Copy link
Member

@poyea poyea Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate different algorithms into different PRs.

Copy link
Contributor Author

@meg-1 meg-1 Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you! i created new branches and comitted them seperately. it seemed to work. is it possible that i might've made a mistake while comitting?? i'm not sure how to check the result of the commit so i might've not seen that they commited as a group.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So @meg-1, what you may try is to create 1 branch for 1 file (with its related changes if it applies). You may:

  1. checkout to the master branch
  2. switch to a new branch, say average-welford
  3. git add and git commit only maths/average_welford.py
  4. check using git status
  5. push it to your repository; and open a PR for that branch
  6. repeat 1-5 for other files

If you have one commit for each file, it's very easy to do this with cherry-pick otherwise you'll need some rebase which is a bit complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you, @poyea, for the instruction! that's what i did before in my previous prs ( #6052 #6061 #6077 ). are they being displayed as one joined commit, with 3 files in it? if so then im not sure what could have gone wrong when i commited them :')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. They are (you can see it in Files changed tab). If you follow the push workflow above, you get one file in each branch, linked to one PR @meg-1

Copy link
Contributor Author

@meg-1 meg-1 Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. They are (you can see it in Files changed tab). If you follow the push workflow above, you get one file in each branch, linked to one PR @meg-1

ty for clarification. just checked and somehow they really did merge into one commit, 'is narcissistic' [ this pr, #6052 ]. i remember you said that the CI can sometimes not work properly, and i think this is the case for me. i did all prs exactly by the instruction steps which you recommended above, and it still merged into one pr. would you please suggest a further course of action, or any possible solutions, since i've been trying to commit for quite a long time, and even though i've been following all steps exactly, nothing seems to go through. @poyea

"""
>>> average_welford([1, 2, 3, 4, 5])
3.0
>>> average_welford([1.2329435, 2.209462409, 3.230925, 4.47626462, 5.2938759204])
3.2886942898799996
>>> average_welford([-57386462.2329435, 2246262.209462409, 4632463.230925, 856737354.47626462, -243664265.2938759204])
112513070.4779665
"""
avg = 0.0
"""while looping through the list,
we calculate the average of the
current element and the average
of the elements before it"""
for index in range(0, len(values)):
avg += (values[index]-avg)/(index+1)
return avg

if __name__ == "__main__":
import doctest
doctest.testmod()
24 changes: 24 additions & 0 deletions maths/is_narcissistic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def is_narcissistic(i: int) -> bool:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i


"""
>>> is_narcissistic(1527)
False
>>> is_narcissistic(-1527)
False
>>> is_narcissistic(153)
True
>>> is_narcissistic("153")
True
"""

"""In number theory, a narcissistic number is a number that is
the sum of its own digits each raised to the power of the number of digits.
this alogrithm finds out if given number 'i' is narcissistic or not"""

i = abs(int(i))
return sum([int(char)**len(str(i)) for char in str(i)]) == i


if __name__ == "__main__":
import doctest
doctest.testmod()
26 changes: 26 additions & 0 deletions strings/censor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def censor(txt: str, word: str) -> str:
"""
>>> censor("one two three", "one")
*** two three
>>> censor("email, password", "password")
email, ********
>>> censor("pin: 2536", "2536")
pin: ****

"""

txt_lst = txt.split()
#here we loop through a list which consists of words from the text we should censor
for index, word1 in enumerate(txt_lst):
if word1 == word:
txt_lst[index] = "*" * len(word1)
txt = ""
#here we combine all of the split words in txt_lst into one string
for word1 in txt_lst:
txt += word1 + " "
txt = txt[:-1]
print(txt)

if __name__ == "__main__":
import doctest
doctest.testmod()
38 changes: 38 additions & 0 deletions strings/decrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def decrypt(txt: str, symbol: str) -> str:
"""
>>> decrypt("o$ne", "$")
one
>>> decrypt("dec@rypt", "@")
decrypt
>>> decrypt("p#ython cod#e", "#")
python code

"""
#here we split the string we need to decypher into a list
txt_lst = txt.split()
res_str = ""
#we create a for loop which loops through the newly created list
for word1 in txt_lst:
#here we convert the current word we got through the loop into a list of charachters
word1_lst = list(word1)
#we loop through each charachter in the newly created list of charachters form the word we are looping throuh
for char1 in word1_lst:
#we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it
if char1 == symbol:
#we find the charachters index from the word
indx = word1.index(char1)
#we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list.
word1_lst[indx] = ""
res_wrd = ""
#we loop throuh the list containing all the charachters from the word, and append those charachters to a new string
for char1 in word1_lst:
res_wrd += char1
#here we add the newly created, decyphered word to a new string which will contain our full result
res_str += res_wrd + " "
res_str = res_str[:-1]
#and here we print out the final result
print(res_str)

if __name__ == "__main__":
import doctest
doctest.testmod()