Skip to main content

Hello, python

0 likes • Jan 20, 2021 • 2 views
Python
Loading...

More Python Posts

Palindrome checker

0 likes • Nov 19, 2022 • 3 views
Python
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")

Binary search

0 likes • Sep 22, 2023 • 11 views
Python
# Python binary search function
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
# Usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
result = binary_search(arr, target)
if result != -1:
print(f"Element is present at index {result}")
else:
print("Element is not present in array")

Append to a file

0 likes • Jun 1, 2023 • 0 views
Python
filename = "data.txt"
data = "Hello, World!"
with open(filename, "a") as file:
file.write(data)

guacamole LDAP creation

0 likes • Nov 18, 2022 • 0 views
Python
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

convert bytes to a string

0 likes • Jun 1, 2023 • 0 views
Python
bytes_data = b'Hello, World!'
string_data = bytes_data.decode('utf-8')
print("String:", string_data)

Binary search algorithm

0 likes • Nov 19, 2022 • 0 views
Python
""" Binary Search Algorithm
----------------------------------------
"""
#iterative implementation of binary search in Python
def binary_search(a_list, item):
"""Performs iterative binary search to find the position of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""
first = 0
last = len(a_list) - 1
while first <= last:
i = (first + last) / 2
if a_list[i] == item:
return ' found at position '.format(item=item, i=i)
elif a_list[i] > item:
last = i - 1
elif a_list[i] < item:
first = i + 1
else:
return ' not found in the list'.format(item=item)
#recursive implementation of binary search in Python
def binary_search_recursive(a_list, item):
"""Performs recursive binary search of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""
first = 0
last = len(a_list) - 1
if len(a_list) == 0:
return ' was not found in the list'.format(item=item)
else:
i = (first + last) // 2
if item == a_list[i]:
return ' found'.format(item=item)
else:
if a_list[i] < item:
return binary_search_recursive(a_list[i+1:], item)
else:
return binary_search_recursive(a_list[:i], item)