Skip to content

Commit dcb9711

Browse files
committed
Implement MyStringReader and MyLimitReader with example usage in main function
1 parent 94c9ebb commit dcb9711

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "io"
4+
5+
type MyStringReader struct {
6+
s string
7+
}
8+
9+
func (r *MyStringReader) Read(p []byte) (n int, err error) {
10+
if len(r.s) == 0 {
11+
return 0, io.EOF
12+
}
13+
n = copy(p, r.s)
14+
r.s = r.s[n:]
15+
return n, nil
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "io"
4+
5+
type MyLimitReader struct {
6+
r io.Reader // underlying reader
7+
remaining int64 // how many bytes left to read
8+
}
9+
10+
func (l *MyLimitReader) Read(p []byte) (n int, err error) {
11+
if l.remaining <= 0 {
12+
return 0, io.EOF
13+
}
14+
15+
// shrink p if it’s bigger than remaining
16+
if int64(len(p)) > l.remaining {
17+
p = p[:l.remaining]
18+
}
19+
20+
n, err = l.r.Read(p)
21+
l.remaining -= int64(n)
22+
return n, err
23+
}
24+
25+
// Constructor function
26+
func LimitReader(r io.Reader, n int64) io.Reader {
27+
return &MyLimitReader{r: r, remaining: n}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
)
7+
8+
// func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)
9+
10+
// func Printf(format string, a ...interface{}) (n int, err error) {
11+
// return Fprintf(os.Stdout, format, a...)
12+
// }
13+
14+
// func Sprintf(format string, args ...interface{}) string {
15+
// var buf bytes.Buffer
16+
// Fprintf(&buf, format, args...)
17+
// return buf.String()
18+
// }
19+
20+
func main() {
21+
r := &MyStringReader{s: "Hello, Reader!"}
22+
limited := LimitReader(r, 5) // li
23+
buf := make([]byte, 10) // read in chunks
24+
for {
25+
n, err := limited.Read(buf)
26+
if n > 0 {
27+
fmt.Printf("Read: %q\n", buf[:n])
28+
}
29+
if err == io.EOF {
30+
fmt.Println("Reached limit!")
31+
break
32+
}
33+
}
34+
}

interface/golinuxcloud/main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type salaryCalculator interface {
6+
calculateSalary() float64
7+
report()
8+
}
9+
10+
type PermanentEmployee struct {
11+
id int
12+
basicSalary float64
13+
commission float64
14+
}
15+
16+
type ContractEmployee struct {
17+
id int
18+
basicSalary float64
19+
}
20+
21+
func (p PermanentEmployee) calculateSalary() float64 {
22+
return p.basicSalary + (p.commission/100)*p.basicSalary
23+
}
24+
25+
func (c ContractEmployee) calculateSalary() float64 {
26+
return c.basicSalary
27+
}
28+
29+
func (p PermanentEmployee) report() {
30+
fmt.Printf("Employee ID %d earns USD %f per month \n", p.id, p.calculateSalary())
31+
}
32+
33+
func (c ContractEmployee) report() {
34+
fmt.Printf("Employee ID %d earns USD %f per month \n", c.id, c.calculateSalary())
35+
}
36+
37+
func main() {
38+
p1 := PermanentEmployee{id: 1, basicSalary: 2300, commission: 13}
39+
p2 := PermanentEmployee{id: 2, basicSalary: 1500, commission: 18}
40+
p3 := PermanentEmployee{id: 3, basicSalary: 2300, commission: 10}
41+
c1 := ContractEmployee{id: 4, basicSalary: 500}
42+
c2 := ContractEmployee{id: 5, basicSalary: 1100}
43+
c3 := ContractEmployee{id: 6, basicSalary: 700}
44+
45+
employees := []salaryCalculator{p1, p2, p3, c1, c2, c3}
46+
47+
var totalSalary float64
48+
49+
for _, employee := range employees {
50+
totalSalary += employee.calculateSalary()
51+
}
52+
fmt.Printf("Company total salary is : USD %f", totalSalary)
53+
}

q1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def divide_numbers_safe():
2+
numerator = int(input("Enter numerator: "))
3+
denominator = int(input("Enter denominator: "))
4+
5+
if denominator == 0:
6+
print("Error: Division by zero is not allowed.")
7+
else:
8+
result = numerator / denominator
9+
print("Result:", result)
10+
11+
12+
if __name__ == "__main__":
13+
divide_numbers_safe()

0 commit comments

Comments
 (0)