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 62937d5

Browse files
adding two other files, showing efficient and not efficient way
1 parent 053f26b commit 62937d5

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# If you want to run the below code, you need a python library
2+
# pip3 install multipledispatch
3+
4+
5+
6+
from multipledispatch import dispatch
7+
8+
#passing one parameter
9+
@dispatch(int,int) # this is called as `property`
10+
def product(first,second):
11+
result = first*second
12+
print(result);
13+
14+
#passing two parameters
15+
@dispatch(int,int,int)
16+
def product(first,second,third):
17+
result = first * second * third
18+
print(result);
19+
20+
#you can also pass data type of any value as per requirement
21+
@dispatch(float,float,float)
22+
def product(first,second,third):
23+
result = first * second * third
24+
print(result);
25+
26+
27+
#calling product method with 2 arguments
28+
product(2,3,2) #this will give output of 12
29+
product(2.2,3.4,2.3) # this will give output of 17.985999999999997
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
1. Reason for not using class?
3+
Because,
4+
'''
5+
6+
# Function to take multiple arguments
7+
def add(datatype, *args):
8+
9+
# if datatype is int
10+
# initialize answer as 0
11+
if datatype =='int':
12+
answer = 0
13+
14+
# if datatype is str
15+
# initialize answer as ''
16+
if datatype =='str':
17+
answer =''
18+
19+
# Traverse through the arguments
20+
for x in args:
21+
22+
# This will do addition if the
23+
# arguments are int. Or concatenation
24+
# if the arguments are str
25+
answer = answer + x
26+
27+
print(answer)
28+
29+
30+
# Integer
31+
add('int', 5, 6)
32+
33+
# String
34+
add('str', 'Hi ', 'Geeks')

0 commit comments

Comments
(0)

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