Skip to main content

Binary search algorithm

Nov 19, 2022CodeCatch
Loading...

More Python Posts

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

Untitled

Jul 24, 2024AustinLeath

0 likes • 0 views

from statistics import median, mean, mode
def print_stats(array):
print(array)
print("median =", median(array))
print("mean =", mean(array))
print("mode =", mode(array))
print()
print_stats([1, 2, 3, 3, 4])
print_stats([1, 2, 3, 3])

AnyTree Randomizer

Apr 15, 2021NoahEaton

0 likes • 0 views

import anytree as at
import random as rm
# Generate a tree with node_count many nodes. Each has a number key that shows when it was made and a randomly selected color, red or white.
def random_tree(node_count):
# Generates the list of nodes
nodes = []
for i in range(node_count):
test = rm.randint(1,2)
if test == 1:
nodes.append(at.Node(str(i),color="white"))
else:
nodes.append(at.Node(str(i),color="red"))
#Creates the various main branches
for i in range(node_count):
for j in range(i, len(nodes)):
test = rm.randint(1,len(nodes))
if test == 1 and nodes[j].parent == None and (not nodes[i] == nodes[j]):
nodes[j].parent = nodes[i]
#Collects all the main branches into a single tree with the first node being the root
for i in range(1, node_count):
if nodes[i].parent == None and (not nodes[i] == nodes[0]):
nodes[i].parent = nodes[0]
return nodes[0]

convert bytes to a string

Jun 1, 2023CodeCatch

0 likes • 1 view

bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

Plotting Fibonacci

Nov 19, 2022CodeCatch

0 likes • 0 views

# Python program for Plotting Fibonacci
# spiral fractal using Turtle
import turtle
import math
def fiboPlot(n):
a = 0
b = 1
square_a = a
square_b = b
# Setting the colour of the plotting pen to blue
x.pencolor("blue")
# Drawing the first square
x.forward(b * factor)
x.left(90)
x.forward(b * factor)
x.left(90)
x.forward(b * factor)
x.left(90)
x.forward(b * factor)
# Proceeding in the Fibonacci Series
temp = square_b
square_b = square_b + square_a
square_a = temp
# Drawing the rest of the squares
for i in range(1, n):
x.backward(square_a * factor)
x.right(90)
x.forward(square_b * factor)
x.left(90)
x.forward(square_b * factor)
x.left(90)
x.forward(square_b * factor)
# Proceeding in the Fibonacci Series
temp = square_b
square_b = square_b + square_a
square_a = temp
# Bringing the pen to starting point of the spiral plot
x.penup()
x.setposition(factor, 0)
x.seth(0)
x.pendown()
# Setting the colour of the plotting pen to red
x.pencolor("red")
# Fibonacci Spiral Plot
x.left(90)
for i in range(n):
print(b)
fdwd = math.pi * b * factor / 2
fdwd /= 90
for j in range(90):
x.forward(fdwd)
x.left(1)
temp = a
a = b
b = temp + b
# Here 'factor' signifies the multiplicative
# factor which expands or shrinks the scale
# of the plot by a certain factor.
factor = 1
# Taking Input for the number of
# Iterations our Algorithm will run
n = int(input('Enter the number of iterations (must be > 1): '))
# Plotting the Fibonacci Spiral Fractal
# and printing the corresponding Fibonacci Number
if n > 0:
print("Fibonacci series for", n, "elements :")
x = turtle.Turtle()
x.speed(100)
fiboPlot(n)
turtle.done()
else:
print("Number of iterations must be > 0")

Find Coin

Oct 4, 2023AustinLeath

0 likes • 10 views

weigh = lambda a,b: sum(b)-sum(a)
FindCoin = lambda A: 0 if (n := len(A)) == 1 else (m := n//3) * (w := 1 + weigh(A[:m], A[2*m:])) + FindCoin(A[m*w:m*(w+1)])
print(FindCoin([1,1,1,1,1,1,1,2,1]))