commit a90eb2b48bce7c8fcfb2c95f4d3dd72fc900948c Author: Muhammad Nauman Raza Date: Fri Dec 13 10:56:25 2024 +0000 chore: add existing programs diff --git a/queue-golang/go.mod b/queue-golang/go.mod new file mode 100644 index 0000000..b5cd84f --- /dev/null +++ b/queue-golang/go.mod @@ -0,0 +1,28 @@ +module devraza/queue + +go 1.23.3 + +require ( + github.com/charmbracelet/bubbles v0.20.0 + github.com/charmbracelet/bubbletea v1.2.4 + github.com/charmbracelet/lipgloss v1.0.0 +) + +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/x/ansi v0.4.5 // indirect + github.com/charmbracelet/x/term v0.2.1 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/sync v0.9.0 // indirect + golang.org/x/sys v0.27.0 // indirect + golang.org/x/text v0.3.8 // indirect +) diff --git a/queue-golang/go.sum b/queue-golang/go.sum new file mode 100644 index 0000000..bbfb7de --- /dev/null +++ b/queue-golang/go.sum @@ -0,0 +1,41 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE= +github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU= +github.com/charmbracelet/bubbletea v1.2.4 h1:KN8aCViA0eps9SCOThb2/XPIlea3ANJLUkv3KnQRNCE= +github.com/charmbracelet/bubbletea v1.2.4/go.mod h1:Qr6fVQw+wX7JkWWkVyXYk/ZUQ92a6XNekLXa3rR18MM= +github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg= +github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo= +github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM= +github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= +github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= +github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= +golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= diff --git a/queue-golang/main.go b/queue-golang/main.go new file mode 100644 index 0000000..013b708 --- /dev/null +++ b/queue-golang/main.go @@ -0,0 +1,146 @@ +package main + +import ( + "fmt" + "log" + "strconv" + "strings" + + "github.com/charmbracelet/bubbles/textinput" + "github.com/charmbracelet/lipgloss" + tea "github.com/charmbracelet/bubbletea" +) + +var ( + command = lipgloss.NewStyle().Foreground(lipgloss.Color("12")).Background(lipgloss.Color("235")) + list = lipgloss.NewStyle().Bold(true) + padding = lipgloss.NewStyle().PaddingTop(1).PaddingLeft(2) + red = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9")) + green = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("10")) +) + +func enqueue(queue []int64, number string) []int64 { + integer, err := strconv.ParseInt(number, 10, 64) + if err != nil { + fmt.Println("Error:", err) + } + + if len(queue) < 5 { + queue = append([]int64{integer}, queue...) + } + + return queue +} + +func dequeue(queue []int64) []int64 { + if len(queue) > 0 { + queue = queue[:len(queue)-1] + } + return queue +} + +func check(queue []int64) string { + if len(queue) == 0 { + return fmt.Sprintf("The queue is currently %v", green.Render("empty")) + } else if len(queue) == 5 { + return fmt.Sprintf("The queue is currently %v", red.Render("full")) + } else { + return fmt.Sprintf("The queue is currently neither %v nor %v", green.Render("empty"), red.Render("full")) + } +} + +type ( + errMsg error +) + +type model struct { + textInput textinput.Model + queue []int64 + status string + show bool + err error +} + +func initialModel() model { + ti := textinput.New() + ti.Placeholder = "enqueue 9" + ti.Focus() + ti.CharLimit = 156 + ti.Width = 20 + + return model{ + textInput: ti, + err: nil, + } +} + +func (m model) Init() tea.Cmd { + return textinput.Blink +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmd tea.Cmd + + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.Type { + case tea.KeyCtrlC: + return m, tea.Quit + case tea.KeyEnter: + arguments := strings.Split(m.textInput.Value(), " ") + switch arguments[0] { + case "enqueue": + m.show = false + m.queue = enqueue(m.queue, arguments[1]) + case "dequeue": + m.show = false + m.queue = dequeue(m.queue) + case "check": + m.show = true + m.status = check(m.queue) + default: + m.show = true + m.status = red.Render("This action is unsupported") + } + } + + case errMsg: + m.err = msg + return m, nil + } + + m.textInput, cmd = m.textInput.Update(msg) + return m, cmd +} + +func (m model) View() string { + command_list := fmt.Sprintf( + "Valid commands: %s, %s, %s", + command.Render(" enqueue "), + command.Render(" dequeue "), + command.Render(" check "), + ) + + if m.show { + return padding.Render(fmt.Sprintf( + "%v\n\n%v\n\n%s", + command_list, + m.status, + m.textInput.View(), + )) + "\n" + } else { + return padding.Render(fmt.Sprintf( + "%v\n\n%v\n\n%s", + command_list, + list.Render(fmt.Sprintf("%v", m.queue)), + m.textInput.View(), + )) + "\n" + } +} + +func main() { + p := tea.NewProgram(initialModel()) + if _, err := p.Run(); err != nil { + log.Fatal(err) + } +} diff --git a/queue-golang/queue b/queue-golang/queue new file mode 100755 index 0000000..10a6dd7 Binary files /dev/null and b/queue-golang/queue differ diff --git a/queue-golang/queue.exe b/queue-golang/queue.exe new file mode 100755 index 0000000..fae6f9c Binary files /dev/null and b/queue-golang/queue.exe differ diff --git a/stack-rs/stack b/stack-rs/stack new file mode 100755 index 0000000..9d10be5 Binary files /dev/null and b/stack-rs/stack differ diff --git a/stack-rs/stack.rs b/stack-rs/stack.rs new file mode 100644 index 0000000..571db06 --- /dev/null +++ b/stack-rs/stack.rs @@ -0,0 +1,81 @@ +use std::io::{self, Write}; + +fn tail(stack: [i64; 5]) -> usize { + let mut index = 0; + for i in 0..=4 { + let subtract = -1*i as isize; + let wanted_index = (4+subtract) as usize; + + if subtract <= 0 && stack[wanted_index] != 0 { + index = wanted_index; + break; + } + } + index +} + +fn push(stack: &mut [i64; 5], number: &mut String) { + let mut index = 0; + let mut used = 0; + + for (idx, i) in stack.clone().iter().enumerate() { + if *i == 0 { + index = idx; + break; + } else { + used += 1; + } + } + if used < 5 { + stack[index] = number.parse().unwrap(); + } else { + eprintln!("\x1b[31m\x1b[1m\nThe stack is full!\x1b[0m"); + } +} + +fn main() { + let mut stack: [i64; 5] = [0; 5]; + + println!("\x1b[32m\x1b[1m\n Push, peek, pop or quit\x1b[0m"); + println!(" When pushing, append the value to push to the command: `push x`\n"); + let mut arguments: Vec<&str> = vec!["0"]; + let mut action = String::new(); + let mut argument: String; + + while arguments[0] != "quit" { + print!("\x1b[36m$\x1b[0m "); + io::stdout().flush().unwrap(); + + io::stdin().read_line(&mut action).expect("Failed to read input"); + + argument = action.clone().replace("\n", ""); + arguments = argument.split(' ').collect::>(); + + action = String::new(); + + match arguments[0] { + "push" => { + push(&mut stack, &mut arguments[1].to_string()); + println!("\n\x1b[1m{:?}\x1b[0m\n", stack); + } + "peek" => { + println!("\n\x1b[1m{}\x1b[0m\n", stack[tail(stack)]); + } + "pop" => { + let used: Vec = stack.iter().filter(|&&x| x != 0).cloned().collect(); + if used.len() > 0 { + stack[tail(stack)] = 0; + eprintln!("\n\x1b[1m{:?}\x1b[0m\n", stack); + } else { + eprintln!("\x1b[31m\x1b[1m\nThe stack is empty!\x1b[0m\n"); + } + } + "quit" => { + std::process::exit(0); + } + _ => { + eprintln!("\x1b[31m\x1b[1m\nThis action is not supported!\x1b[0m\n"); + } + } + } +}