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 6140ffa

Browse files
committed
fixes #38
1 parent 4c5c04d commit 6140ffa

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

‎algorithm/README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
#Algorithm
1+
#Algorithm
2+
3+
Before diving to algorithm, we need to learn about problem solving approaches or strategies.
4+
Some of the well known approaches are following,
5+
6+
## Divide and conquer
7+
8+
Divide and conquer basically does three jobs, first divides the problem into smallest units recursively, then solves those units and last of all, combines those solved units.
9+
10+
### Some Algorithms
11+
- Binary Search
12+
13+
The pre-requisites of binary search is, the array needs to be sorted.
14+
15+
Code Sample
16+
```
17+
func binaySearch(nums[]int, left int, right int, target int,index int) int{
18+
if(left>right){
19+
return index
20+
}
21+
mid:=(left+right)/2
22+
if(nums[mid]<target){
23+
left=mid+1
24+
index=left
25+
return helper(nums,left,right,target,index)
26+
}else if(nums[mid]>target){
27+
right=mid-1
28+
return helper(nums,left,right,target,index)
29+
}else{
30+
return mid
31+
}
32+
}
33+
```
34+
35+
36+
- Merge Sort
37+
- Quick Sort
38+
39+
## Some Problems
40+
- Longest Common Prefix
41+
- Median of Two Sorted Arrays
42+
- Maximum Subarray
43+
- Majority Element
44+
- Kth Largest Element in an Array
45+
- Search in Rotated Sorted Array
46+
- Find First and Last Position of Element in Sorted Array
47+
- Search Insert Position
48+
- Two Sum II - Input array is sorted
49+
50+
51+
## Greedy approach
52+
## Dynamic programming
53+
## Backtracking
54+
## Branch and bound

‎design-pattern/behavioral/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# design-pattern
2+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type subject interface {
6+
register(Observer observer)
7+
deregister(Observer observer)
8+
notifyAll()
9+
}
10+
11+
type item struct {
12+
observerList []observer
13+
name string
14+
inStock bool
15+
}
16+
17+
func newItem(name string) *item {
18+
return &item{
19+
name: name,
20+
}
21+
}
22+
23+
func (i *item) updateAvailability() {
24+
fmt.Printf("Item %s is now in stock\n", i.name)
25+
i.inStock = true
26+
i.notifyAll()
27+
}
28+
29+
func (i *item) register(o observer) {
30+
i.observerList = append(i.observerList, o)
31+
}
32+
33+
func (i *item) deregister(o observer) {
34+
i.observerList = removeFromslice(i.observerList, o)
35+
}
36+
37+
func (i *item) notifyAll() {
38+
for _, observer := range i.observerList {
39+
observer.update(i.name)
40+
}
41+
}
42+
43+
func removeFromslice(observerList []observer, observerToRemove observer) []observer {
44+
observerListLength := len(observerList)
45+
for i, observer := range observerList {
46+
if observerToRemove.getID() == observer.getID() {
47+
observerList[observerListLength-1], observerList[i] = observerList[i], observerList[observerListLength-1]
48+
return observerList[:observerListLength-1]
49+
}
50+
}
51+
return observerList
52+
}
53+
54+
type observer interface {
55+
update(string)
56+
getID() string
57+
}
58+
59+
type customer struct {
60+
id string
61+
}
62+
63+
func (c *customer) update(itemName string) {
64+
fmt.Printf("Sending email to customer %s for item %s\n", c.id, itemName)
65+
}
66+
67+
func (c *customer) getID() string {
68+
return c.id
69+
}
70+
71+
func main() {
72+
shirtItem := newItem("Nike Shirt")
73+
observerFirst := &customer{id: "abc@gmail.com"}
74+
observerSecond := &customer{id: "xyz@gmail.com"}
75+
shirtItem.register(observerFirst)
76+
shirtItem.register(observerSecond)
77+
shirtItem.updateAvailability()
78+
}

0 commit comments

Comments
(0)

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