Skip to content

Commit dc13838

Browse files
committed
Major Typo Fix - Wrong Solution between 1342 and 1108
1 parent 604c880 commit dc13838

File tree

2 files changed

+84
-34
lines changed

2 files changed

+84
-34
lines changed

Defanging IP Address/EfficientSolution.py

+20-31
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
## Leetcode
33
## Student: Vandit Jyotindra Gajjar
44
## Year: 2020
5-
## Problem: 1342
6-
## Problem Name: Number of Steps to Reduce a Number to Zero
5+
## Problem: 1108
6+
## Problem Name: Defanging an IP Address
77
##===================================
88
#Given a non-negative integer num, return the number of steps to reduce it to zero.
99
#
@@ -34,36 +34,25 @@
3434
#
3535
#Input: num = 123
3636
#Output: 12
37-
#
38-
#The solution for this problem is fairly starightforward by following the problem statement.
3937

4038
class Solution:
4139

42-
def numberOfSteps (self, number):
43-
44-
step = 0 #Initialize our step counter.
45-
46-
while number != 0: #While the number is not zero (Following the condition of non-negative integer)
47-
48-
if number % 2 == 0: #Condition Check - If number is divided by zero and gives no remainder
49-
50-
number = number // 2 #We'll divide the number by 2 again. Here (//) will give you integer as in 2.0, 3.0 etc. In python (/) will give you floating numbers.
51-
52-
else: #If condition fails then
53-
54-
number = number - 1 #We'll subtract the number by 1.
55-
56-
step = step + 1 #Corresponding, we'll update our step counter.
57-
58-
return step #We'll return the step number at the end.
40+
def defangIPaddr(self, address):
41+
42+
string = [] #[]
43+
44+
for character in address:
45+
46+
if character == '.': #1.1.1.1 if . is in address
47+
48+
string.append("[.]") #we'll append it by ["[.]"]
49+
50+
else:
51+
52+
string.append(character) #Or we'll append it with characters
5953

60-
#For example: number = 3
61-
# 3 % 2 = 1 (If condition failed.)
62-
# 3 - 1 = 2 (Enters the else condition.) [step = 1]
63-
# Now number = 2.
64-
# 2 % 2 = 0 (If condition pass.)
65-
# 2 // 2 = 1.0 (Enters the if condition.) [step = 2]
66-
# number = 1.0
67-
# 1 % 2 = -1 (if condition failed.)
68-
# 1 - 1 = 0 (Enters the else condition.) (Goal state reached.) [step = 3]
69-
# Total number of step = 3.
54+
return "".join(string) #Finally we'll join the string which will give "1[.]1[.]1[.]1"
55+
.
56+
#Initialize empty string
57+
#For every character in our IP Address, we will check with the condition that if "." is in the address, we'll simply replace with our given value which is "[.]",
58+
#otherwise, we'll append remaining address. Lastly, we'll simply join all the character in string.

Number_Step_To_Zero/EfficientSolution.py

+64-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,67 @@
22
## Leetcode
33
## Student: Vandit Jyotindra Gajjar
44
## Year: 2020
5-
## Problem: 1108
6-
## Problem Name: Defanging an IP Address
7-
##===================================
5+
## Problem: 1342
6+
## Problem Name: Number of Steps to Reduce a Number to Zero
7+
##===================================
8+
#Given a non-negative integer num, return the number of steps to reduce it to zero.
9+
#
10+
#If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
11+
#Example 1:
12+
#
13+
#Input: num = 14
14+
#Output: 6
15+
#Explanation:
16+
#Step 1) 14 is even; divide by 2 and obtain 7.
17+
#Step 2) 7 is odd; subtract 1 and obtain 6.
18+
#Step 3) 6 is even; divide by 2 and obtain 3.
19+
#Step 4) 3 is odd; subtract 1 and obtain 2.
20+
#Step 5) 2 is even; divide by 2 and obtain 1.
21+
#Step 6) 1 is odd; subtract 1 and obtain 0.
22+
#
23+
#Example 2:
24+
#
25+
#Input: num = 8
26+
#Output: 4
27+
#Explanation:
28+
#Step 1) 8 is even; divide by 2 and obtain 4.
29+
#Step 2) 4 is even; divide by 2 and obtain 2.
30+
#Step 3) 2 is even; divide by 2 and obtain 1.
31+
#Step 4) 1 is odd; subtract 1 and obtain 0.
32+
#
33+
#Example 3:
34+
#
35+
#Input: num = 123
36+
#Output: 12
37+
#
38+
#The solution for this problem is fairly starightforward by following the problem statement.
39+
40+
class Solution:
41+
42+
def numberOfSteps (self, number):
43+
44+
step = 0 #Initialize our step counter.
45+
46+
while number != 0: #While the number is not zero (Following the condition of non-negative integer)
47+
48+
if number % 2 == 0: #Condition Check - If number is divided by zero and gives no remainder
49+
50+
number = number // 2 #We'll divide the number by 2 again. Here (//) will give you integer as in 2.0, 3.0 etc. In python (/) will give you floating numbers.
51+
52+
else: #If condition fails then
53+
54+
number = number - 1 #We'll subtract the number by 1.
55+
56+
step = step + 1 #Corresponding, we'll update our step counter.
57+
58+
return step #We'll return the step number at the end.
59+
60+
#For example: number = 3
61+
# 3 % 2 = 1 (If condition failed.)
62+
# 3 - 1 = 2 (Enters the else condition.) [step = 1]
63+
# Now number = 2.
64+
# 2 % 2 = 0 (If condition pass.)
65+
# 2 // 2 = 1.0 (Enters the if condition.) [step = 2]
66+
# number = 1.0
67+
# 1 % 2 = -1 (if condition failed.)
68+
# 1 - 1 = 0 (Enters the else condition.) (Goal state reached.) [step = 3]

0 commit comments

Comments
 (0)