Skip to content
New issue

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

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

Already on GitHub? # to your account

Python program for product of two numbers and Linear search program in python #372

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@
"doc",
"code"
]
},
{
"login": "NormalVad",
"name": "NormalVad",
"avatar_url": "https://avatars2.githubusercontent.com/u/73097046?v=4",
"profile": "https://github.com/NormalVad",
"contributions": [
"doc",
"code"
]
}
],
"commitConvention": "none"
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
<td align="center"><a href="https://github.com/gaurav01k3"><img src="https://avatars0.githubusercontent.com/u/67451786?v=4?s=100" width="100px;" alt=""/><br /><sub><b>GAURAV KUMAR</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=gaurav01k3" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=gaurav01k3" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/wboccard"><img src="https://avatars3.githubusercontent.com/u/72316984?v=4?s=100" width="100px;" alt=""/><br /><sub><b>wboccard</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=wboccard" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=wboccard" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/d-l-mcbride"><img src="https://avatars3.githubusercontent.com/u/46550732?v=4?s=100" width="100px;" alt=""/><br /><sub><b>d-l-mcbride</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=d-l-mcbride" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=d-l-mcbride" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/NormalVad"><img src="https://avatars2.githubusercontent.com/u/73097046?v=4" width="100px;" alt=""/><br /><sub><b>NormalVad</b></sub></a><br /><a href="https://github.com/CodeToExpress/dailycodebase/commits?author=NormalVad" title="Documentation">📖</a> <a href="https://github.com/CodeToExpress/dailycodebase/commits?author=NormalVad" title="Code">💻</a></td>
</tr>
</table>

Expand Down
28 changes: 28 additions & 0 deletions day14/Python/Product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
@author: NormalVad
@date: 25/10/2020
"""

def product(a,b):

Choose a reason for hiding this comment

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

There was another part to the question where you needed to calculate the sum of the digits of a number using recursion. Do include the solution to that in this Python script as well.

if(a==0 or b==0):
return 0
elif(b == 1):
return a
elif(a<0 and b<0):
a = a*(-1)
b = b*(-1)
return a + product(a,b-1)
elif(a>0 and b>0):
return a+product(a,b-1)
elif(a<0 and b>0):
a = a*(-1)
x = a+product(a,b-1)
return x*(-1)
else:
b = b*(-1)
x = a+product(a,b-1)
return x*(-1)

print("Enter two numbers to be multiplied seperated by a space")
(a,b) = list(map(int, input().split()))
print(product(a,b))
32 changes: 32 additions & 0 deletions day14/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,35 @@ void main(){

}
```
### Python Implementation

#### [Solution](./python/Product.py)
```python
"""
@author:NormalVad
@date:25/10/2020
"""
def product(a,b):
if(a==0 or b==0):
return 0
elif(b == 1):
return a
elif(a<0 and b<0):
a = a*(-1)
b = b*(-1)
return a + product(a,b-1)
elif(a>0 and b>0):
return a+product(a,b-1)
elif(a<0 and b>0):
a = a*(-1)
x = a+product(a,b-1)
return x*(-1)
else:
b = b*(-1)
x = a+product(a,b-1)
return x*(-1)

print("Enter two numbers to be multiplied seperated by a space")
(a,b) = list(map(int, input().split()))
print(product(a,b))
```
26 changes: 26 additions & 0 deletions day28/Python/LinearSearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
@author:NormalVad
@date:25/10/2020
"""


def LinearSearch(a,n):
l = len(a)
found = False
i = 0
while(i<l):
if(a[i]==n):
found = True
break
else:
i += 1
if(found):
return i
else:
return "Undefined"

print("Please input Elements of array in single line seperated by spaces")
arr = list(map(int, input().split()))
print("Input element to be found")
n = int(input())
print(LinearSearch(arr,n))
33 changes: 33 additions & 0 deletions day28/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,36 @@ void main(){
}
}
```
## Python Implementation

### [Solution](./Python/linearSearch.py)
```python

"""
@author:NormalVad
@date:25/10/2020
"""


def LinearSearch(a,n):
l = len(a)
found = False
i = 0
while(i<l):
if(a[i]==n):
found = True
break
else:
i += 1
if(found):
return i
else:
return "Undefined"

print("Please input Elements of array in single line seperated by spaces")
arr = list(map(int, input().split()))
print("Input element to be found")
n = int(input())
print(LinearSearch(arr,n))
```