## CSCE 2100 Question 3

0 likes • Nov 18, 2022
Python

## More Python Posts

```# Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.# For example, if n is 10, the output should be “2, 3, 5, 7”. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19”.

# Python program to print all primes smaller than or equal to# n using Sieve of Eratosthenes
def SieveOfEratosthenes(n):		# Create a boolean array "prime[0..n]" and initialize	# all entries it as true. A value in prime[i] will	# finally be false if i is Not a prime, else true.	prime = [True for i in range(n + 1)]	p = 2	while (p * p <= n):				# If prime[p] is not changed, then it is a prime		if (prime[p] == True):						# Update all multiples of p			for i in range(p * 2, n + 1, p):				prime[i] = False		p += 1	prime[0]= False	prime[1]= False	# Print all prime numbers	for p in range(n + 1):		if prime[p]:			print (p)
# driver programif __name__=='__main__':    n = 30    print("Following are the prime numbers smaller")    print("than or equal to ", n)    print("than or equal to ", n)    SieveOfEratosthenes(n)```
```import osimport sysimport argparseimport jsonimport csvimport getpassimport stringimport randomimport re
from datetime import datetimeimport ldapimport requestsfrom requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)from requests.auth import HTTPBasicAuthimport validators
import ipdb
# ldap functionsdef get_ldap_group_members(ldap_group, user, passwrd, DOMAIN, DOMAIN_SEARCH_BASE):    #DOMAIN_GROUP, DOMAIN_USER, DOMAIN_PASS, DOMAIN, DOMAIN_SEARCH_BASE    '''    Input a user name and search the directory    '''    #---- Setting up the Connection    #account used for binding - Avoid putting these in version control    bindDN = str(user)    bindPass = passwrd
#set some tuneables for the LDAP library.    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_ALLOW)    #ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, CACERTFILE)        conn = ldap.initialize('ldaps://'+str(DOMAIN))    conn.protocol_version = 3    conn.set_option(ldap.OPT_REFERRALS, 0)
#authenticate the connection so that you can make additional queries    try:        result = conn.simple_bind_s(bindDN, bindPass)     except ldap.INVALID_CREDENTIALS:        result = "Invalid credentials for %s" % user        sys.exit()
#build query in the form of (uid=user)    ldap_query = '(cn=' + ldap_group + ')'
query_result = []      lencount = True    range_start = 0    total_active_students_count = 0        while lencount:        #print(len(query_result))        range_end = range_start + 1499        member_range = ['member;range=' + str(range_start) + '-' + str(range_end)]        #print(member_range)        ldap_info = conn.search_s(str(DOMAIN_SEARCH_BASE), ldap.SCOPE_SUBTREE, filterstr=ldap_query, attrlist=member_range)        member_range = next(iter(ldap_info[0][1].keys()))        ldap_objects = ldap_info[0][1][member_range]        print("\n")        for ldap_object in ldap_objects:            decoded_member_string = ldap_object.decode("utf-8")            member_cn = re.split('=|,', decoded_member_string)[1]            print(f"User number: {total_active_students_count} in {ldap_group}: {member_cn}")            query_result.append(member_cn)            total_active_students_count = total_active_students_count + 1                if len(ldap_objects) == 1500:            lencount = True            range_start += 1500            range_end += 1500                else:            lencount = False            print(f"TOTAL USERS IN ActiveStudents: {total_active_students_count}")    return query_result

if __name__ == "__main__":    main()```
```import random
class Node:
def __init__(self, c):
self.left = None        self.right = None        self.color = c        def SetColor(self,c) :        self.color = c
def PrintNode(self) :        print(self.color)
def insert(s, root, i, n):    if i < n:        temp = Node(s[i])         root = temp         root.left = insert(s, root.left,2 * i + 1, n)         root.right = insert(s, root.right,2 * i + 2, n)    return root
def MakeTree(s) :    list = insert(s,None,0,len(s))    return list

def MakeSet() :    s = []    count = random.randint(7,12)    for _ in range(count) :        color = random.randint(0,1) == 0 and "Red" or "White"        s.append(color)    return s
def ChangeColor(root) :    if (root != None) :        if (root.color == "White") :            root.SetColor("Red")        ChangeColor(root.left)        ChangeColor(root.right)

def PrintList(root) :    if root.left != None :        PrintList(root.left)    else :        root.PrintNode()    if root.right != None :        PrintList(root.right)    else :        root.PrintNode()

t1 = MakeTree(MakeSet())print("Original Colors For Tree 1:\n")PrintList(t1)ChangeColor(t1)print("New Colors For Tree 1:\n")PrintList(t1)
t2 = MakeTree(MakeSet())print("Original Colors For Tree 2:\n")PrintList(t2)ChangeColor(t2)print("New Colors For Tree 2:\n")PrintList(t2)
t3 = MakeTree(MakeSet())print("Original Colors For Tree 3:\n")PrintList(t3)ChangeColor(t3)print("New Colors For Tree 3:\n")PrintList(t3)```
```import math
def factorial(n):    print(math.factorial(n))    return (math.factorial(n))    factorial(5)factorial(10)factorial(15)```
```def key_of_min(d):  return min(d, key = d.get)
key_of_min({'a':4, 'b':0, 'c':13}) # b```
```from collections import Counter
def find_parity_outliers(nums):  return [    x for x in nums    if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]  ]
find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]```