File tree 6 files changed +101
-19
lines changed
6 files changed +101
-19
lines changed Original file line number Diff line number Diff line change
1
+ # Implementing Core features
2
+ # Python beyonds the basics - Object oriented programming
3
+
4
+ class SumList (object ):
5
+
6
+ def __init__ (self ,this_list ):
7
+ self .myList = this_list
8
+
9
+ def __add__ (self ,other ):
10
+
11
+ new_list = [ x + y for x ,y in zip (self .myList , other .myList )]
12
+
13
+ return SumList (new_list )
14
+
15
+ def __repr__ (self ):
16
+ return str (self .myList )
17
+
18
+
19
+
20
+
21
+
22
+
Original file line number Diff line number Diff line change
1
+ # SubClassing Built-In's
2
+ # Python beyond the basics - object oriented programming
3
+
4
+ """
5
+ Here we are inheriting from a built in class "dict"
6
+ from which we can access all the properties of the dictionries
7
+ in python and implement our own.
8
+ as you can see in the given example below.
9
+ """
10
+
11
+ class MyDict (dict ):
12
+
13
+ def __setitem__ (self , key , value ):
14
+ print ("Setting a key and value" )
15
+ dict .__setitem__ (self , key , value )
File renamed without changes.
Original file line number Diff line number Diff line change
1
+ import abc
2
+ from datetime import datetime
3
+
4
+ class WriteFile (metaclass = abc .ABCMeta ):
5
+
6
+ @abc .abstractmethod
7
+ def write (self ):
8
+ return
9
+
10
+ def __init__ (self ,filename ):
11
+ self .filename = filename
12
+
13
+ def write_line (self ,text ):
14
+ fh = open (self .filename , 'a' )
15
+ fh .write (text + "\n " )
16
+ fh .close ()
17
+
18
+
19
+ class DelimFile (WriteFile ):
20
+
21
+ def __init__ (self ,filename ,delim ):
22
+ super (DelimFile , self ).__init__ (filename )
23
+ self .delim = delim
24
+
25
+ def write (self , this_list ):
26
+ line = self .delim .join (this_list )
27
+ self .write_line (line )
28
+
29
+ class Logfile (WriteFile ):
30
+
31
+ def write (self ,this_line ):
32
+ dt = datetime .now ()
33
+ date_str = dt .strtime ('%Y-%m-%d %H:%M' )
34
+ self .write_line ('{0} {1}' .format (date_str ,this_line ))
Original file line number Diff line number Diff line change 1
1
#include <stdio.h>
2
- #include <string.h>
3
- #include <math.h>
4
- #include <stdlib.h>
5
2
6
- int main () {
7
-
8
- const int n ,q ;
9
- scanf ("%d" ,& n );
10
- char * names = (char * ) malloc (n * 12 * sizeof (char ));
11
- char * qNames = (char * ) malloc (q * 12 * sizeof (char ));
3
+ struct Distance {
4
+ int feet ;
5
+ float inch ;
6
+ } d1 , d2 , result ;
12
7
13
- for (int i = 0 ; i <= n ; i ++ ){
14
- gets (names );
15
- }
16
- for (int i = 0 ; i <= q ; i ++ ){
17
- gets (qNames );
18
- }
19
-
20
-
8
+ int main () {
9
+ // take first distance input
10
+ printf ("Enter 1st distance\n" );
11
+ printf ("Enter feet: " );
12
+ scanf ("%d" , & d1 .feet );
13
+ printf ("Enter inch: " );
14
+ scanf ("%f" , & d1 .inch );
15
+
16
+ // take second distance input
17
+ printf ("\nEnter 2nd distance\n" );
18
+ printf ("Enter feet: " );
19
+ scanf ("%d" , & d2 .feet );
20
+ printf ("Enter inch: " );
21
+ scanf ("%f" , & d2 .inch );
22
+
23
+ // adding distances
24
+ result .feet = d1 .feet + d2 .feet ;
25
+ result .inch = d1 .inch + d2 .inch ;
21
26
22
- return 0 ;
23
- }
27
+ // convert inches to feet if greater than 12
28
+ while (result .inch >= 12.0 ) {
29
+ result .inch = result .inch - 12.0 ;
30
+ ++ result .feet ;
31
+ }
32
+ printf ("\nSum of distances = %d\'-%.1f\"" , result .feet , result .inch );
33
+ return 0 ;
34
+ }
You can’t perform that action at this time.
0 commit comments