Skip to content

Instantly share code, notes, and snippets.

@Ja7ad
Last active August 6, 2023 05:21
Show Gist options
  • Save Ja7ad/e02d9e8f359e692222485d117ef5d61a to your computer and use it in GitHub Desktop.
Save Ja7ad/e02d9e8f359e692222485d117ef5d61a to your computer and use it in GitHub Desktop.
Lottery Game and Add to number
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"time"
)
type User struct {
UUID string `json:"UUID"`
Participation int `json:"participation"`
}
type LotteryResponse struct {
Prize string `json:"prize"`
}
var (
prizes = []string{"A", "B", "C", "D", "E"}
weights = []float64{0.1, 0.3, 0.2, 0.15, 0.25}
users = make(map[string]*User)
)
func main() {
http.HandleFunc("/api/v1/lottery", handleLotteryRequest)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleLotteryRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var requestBody struct {
UUID string `json:"UUID"`
}
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
user, found := users[requestBody.UUID]
if !found {
user = &User{UUID: requestBody.UUID}
users[requestBody.UUID] = user
}
if user.Participation >= 3 {
http.Error(w, "Exceeded participation limit", http.StatusBadRequest)
return
}
user.Participation++
prize := drawPrize()
response := LotteryResponse{Prize: prize}
jsonResponse, err := json.Marshal(response)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonResponse)
}
func drawPrize() string {
rand.Seed(time.Now().UnixNano())
r := rand.Float64()
var cumulativeWeight float64
for i, weight := range weights {
cumulativeWeight += weight
if r <= cumulativeWeight {
return prizes[i]
}
}
// This should not be reached unless the weights are invalid
return ""
}
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{}
current, carry := dummy, 0
for l1 != nil || l2 != nil {
sum := carry
if l1 != nil {
sum += l1.Val
l1 = l1.Next
}
if l2 != nil {
sum += l2.Val
l2 = l2.Next
}
current.Next = &ListNode{Val: sum % 10}
current = current.Next
carry = sum / 10
}
if carry > 0 {
current.Next = &ListNode{Val: carry}
}
return dummy.Next
}
func main() {
// Example 1
l1 := &ListNode{Val: 2}
l1.Next = &ListNode{Val: 4}
l1.Next.Next = &ListNode{Val: 3}
l2 := &ListNode{Val: 5}
l2.Next = &ListNode{Val: 6}
l2.Next.Next = &ListNode{Val: 4}
result := addTwoNumbers(l1, l2)
printLinkedList(result) // Output: 7 -> 0 -> 8
// Example 2
l3 := &ListNode{Val: 0}
l4 := &ListNode{Val: 0}
result2 := addTwoNumbers(l3, l4)
printLinkedList(result2) // Output: 0
// Example 3
l5 := &ListNode{Val: 9}
l5.Next = &ListNode{Val: 9}
l5.Next.Next = &ListNode{Val: 9}
l5.Next.Next.Next = &ListNode{Val: 9}
l5.Next.Next.Next.Next = &ListNode{Val: 9}
l5.Next.Next.Next.Next.Next = &ListNode{Val: 9}
l5.Next.Next.Next.Next.Next.Next = &ListNode{Val: 9}
l6 := &ListNode{Val: 9}
l6.Next = &ListNode{Val: 9}
l6.Next.Next = &ListNode{Val: 9}
l6.Next.Next.Next = &ListNode{Val: 9}
result3 := addTwoNumbers(l5, l6)
printLinkedList(result3) // Output: 8 -> 9 -> 9 -> 9 -> The code provided above demonstrates the implementation of both challenges in Go. Please note that the code assumes the presence of a `printLinkedList` function to display the linked list values for testing purposes.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment