Skip to main content

Sieve of Eratosthenes

Nov 19, 2022CodeCatch
Loading...

More Python Posts

Hello World

Sep 9, 2023AustinLeath

0 likes • 23 views

print("test")

curry function

Nov 19, 2022CodeCatch

0 likes • 1 view

from functools import partial
def curry(fn, *args):
return partial(fn, *args)
add = lambda x, y: x + y
add10 = curry(add, 10)
add10(20) # 30

LeetCode Flood Fill

Oct 15, 2022CodeCatch

0 likes • 0 views

class Solution(object):
def floodFill(self, image, sr, sc, newColor):
R, C = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r-1, c)
if r+1 < R: dfs(r+1, c)
if c >= 1: dfs(r, c-1)
if c+1 < C: dfs(r, c+1)
dfs(sr, sc)
return image

xxx

Apr 27, 2025hasnaoui1

0 likes • 1 view

import random
import time
def generate_maze(width, height):
"""Generate a random maze using depth-first search"""
maze = [[1 for _ in range(width)] for _ in range(height)]
def carve(x, y):
maze[y][x] = 0
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
random.shuffle(directions)
for dx, dy in directions:
nx, ny = x + dx*2, y + dy*2
if 0 <= nx < width and 0 <= ny < height and maze[ny][nx] == 1:
maze[y + dy][x + dx] = 0
carve(nx, ny)
carve(1, 1)
maze[0][1] = 0 # Entrance
maze[height-1][width-2] = 0 # Exit
return maze
def print_maze(maze, path=None):
"""Print the maze with ASCII characters"""
if path is None:
path = []
for y in range(len(maze)):
for x in range(len(maze[0])):
if (x, y) in path:
print('◍', end=' ')
elif maze[y][x] == 0:
print(' ', end=' ')
else:
print('▓', end=' ')
print()
def solve_maze(maze, start, end):
"""Solve the maze using recursive backtracking"""
visited = set()
path = []
def dfs(x, y):
if (x, y) == end:
path.append((x, y))
return True
if (x, y) in visited or maze[y][x] == 1:
return False
visited.add((x, y))
path.append((x, y))
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
if dfs(x + dx, y + dy):
return True
path.pop()
return False
dfs(*start)
return path
# Generate and solve a maze
width, height = 21, 11 # Should be odd numbers
maze = generate_maze(width, height)
start = (1, 0)
end = (width-2, height-1)
print("Generated Maze:")
print_maze(maze)
print("\nSolving Maze...")
time.sleep(2)
path = solve_maze(maze, start, end)
print("\nSolved Maze:")
print_maze(maze, path)

two-digit integer

Feb 26, 2023wabdelh

0 likes • 0 views

#You are given a two-digit integer n. Return the sum of its digits.
#Example
#For n = 29 the output should be solution (n) = 11
def solution(n):
return (n//10 + n%10)

guacamole LDAP creation

Nov 18, 2022AustinLeath

0 likes • 0 views

import os
import sys
import argparse
import json
import csv
import getpass
import string
import random
import re
from datetime import datetime
import ldap
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from requests.auth import HTTPBasicAuth
import validators
def create_guac_connection(BASE_URL, auth_token, ldap_group, computer, guac_group_id):
'''
creates a guac connection
'''
json_header = {'Accept': 'application/json'}
query_parm_payload = { 'token': auth_token }
payload_data = {
"parentIdentifier":guac_group_id,
"name":computer,
"protocol":"vnc",
"parameters":{
"port":"5900",
"read-only":"",
"swap-red-blue":"",
"cursor":"",
"color-depth":"",
"clipboard-encoding":"",
"disable-copy":"",
"disable-paste":"",
"dest-port":"",
"recording-exclude-output":"",
"recording-exclude-mouse":"",
"recording-include-keys":"",
"create-recording-path":"",
"enable-sftp":"true",
"sftp-port":"",
"sftp-server-alive-interval":"",
"enable-audio":"",
"audio-servername":"",
"sftp-directory":"",
"sftp-root-directory":"",
"sftp-passphrase":"",
"sftp-private-key":"",
"sftp-username":"",
"sftp-password":"",
"sftp-host-key":"",
"sftp-hostname":"",
"recording-name":"",
"recording-path":"",
"dest-host":"",
"password":"asdasd",
"username":"asdasd",
"hostname":"nt72310.cvad.unt.edu"
},
"attributes":{
"max-connections":"",
"max-connections-per-user":"1",
"weight":"",
"failover-only":"",
"guacd-port":"",
"guacd-encryption":"",
"guacd-hostname":""
}
}
CREATE_CONNECTION_URL = BASE_URL + "/api/session/data/mysql/connections"
create_connection_request = requests.post(CREATE_CONNECTION_URL, headers=json_header, params=query_parm_payload, data=payload_data, verify=False)
create_connection_result = create_connection_request.status_code
if create_connection_result == "200":
print("Successfully created computer: " + computer)
else:
print(create_connection_request.json())
return create_connection_result