Skip to content
hakk
  • Home
  • Blog
  • Docs
  • DSA
  • Snippets

Python

  • Ansible - Please add this host's fingerprint to your known_hosts file to manage this host 2023-12-15

    If you’ve ever tried to run an ansible playbook and recieved Please add this host's fingerprint to your known_hosts file to manage this host you’re not alone. Here’s a couple of ways to fix it.

    Turn off host key checking

    This would not be the preferred method but turning off host key checking will enable ansible to continue on with the playbook.

    Either modify the /etc/ansible/ansible.cfg or create an ansible.cfg file in the project directory and add the following lines to it:

  • Install and Configure Embedded Python on Windows 2023-12-09

    I wanted to document these steps after downloading embedded Python recently. I extracted it, set the environment path and started the interpreter. Everything seemed to be going smoothly until I tried to exit exit() and it reported back ’exit not found’ or something like that. What!? How can this be? I ended up import sys and using sys.exit() but this sent me on an adventure to determine how to use embedded Python as a regular install.

  • Best Way to Check if a Number Contains Another Number 2022-06-19

    Need to find out if a number contains another number? And you want better performance than converting to a string and then looping through that to find the key number? Well you’ve come to the right place!

    Using a combination of the modulo operator and division it’s possible to pull the number apart one digit at a time until it reaches 0.

    Implementation Using a Loop

    Python Go JavaScript C++ Java
    def checkNumberIfContainsKey(number, key):
        while number > 0:
            if number % 10 == key:
                 return True
            number = number // 10
        return False
    
    package main
    
    import "fmt"
    
    func checkNumberIfContainsKey(number, key int) bool {
    	for number > 0 {
    		if number%10 == key {
    			return true
    		}
    		number = number / 10
    	}
    	return false
    }
    
    func main() {
    	fmt.Println(checkNumberIfContainsKey(359, 9))
    }
    
    function checkNumberIfContainsKey(number, key){
        while(number > 0){
            if(number%10 == key){
                return true;
            }
            number = Math.floor(number / 10);        
        }
        return false;
    }
    
    #include "bits/stdc++.h"
    using namespace std;
    
    bool checkNumberIfContainsKey(int number, int key)
    {
    	while (number > 0) 
    	{
    		if (number % 10 == key) 
    		{
    			return true;
    		}
    		number = number / 10;
    	}
    	return false;
    }
    
    int main(int argc, char const *argv[])
    {
    	cout << checkNumberIfContainsKey(359,9) << endl;
    	return 0;
    }
    
    class ContainsKey {
        public static boolean checkNumberIfContainsKey(int num, int key) {
            while (num > 0) {
                if (num % 10 == key) {
                    return true;
                }
                num = num / 10;
            }
            return false;
        }
    }
    

    Recursive Implementation

    Python Go JavaScript C++ Java
    def checkNumberIfContainsKey(number, key):
        if number == 0:
            return False
        
        if number % 10 == key:
            return True
        
        return checkNumberIfContainsKey(number // 10, key)
    
    package main
    
    import "fmt"
    
    func checkNumberIfContainsKey(number, key int) bool {
    	if number == 0 {
    		return false
    	}
    	if number%10 == key {
    		return true
    	}
    	return checkNumberIfContainsKey(number / 10, key)
    	
    }
    
    func main() {
    	fmt.Println(checkNumberIfContainsKey(359, 9))
    }
    
    function checkNumberIfContainsKey(number, key){
        if (number === 0) {
        	return false;
        }
        if(number%10 == key) {
        	return true;
        }
        return checkNumberIfContainsKey(Math.floor(number / 10), key);
    }
    
    #include "bits/stdc++.h"
    using namespace std;
    
    bool checkNumberIfContainsKey(int number, int key)
    {
    	while (number > 0) 
    	{
    		if (number % 10 == key) 
    		{
    			return true;
    		}
    		number = number / 10;
    	}
    	return false;
    }
    
    int main(int argc, char const *argv[])
    {
    	cout << checkNumberIfContainsKey(359,9) << endl;
    	return 0;
    }
    
    class ContainsKey {
        public static boolean checkNumberIfContainsKey(int num, int key) {
            if (num == 0) {
                return false;
            }
    
            if (num % 10 == key) {
                return true;
            }
    
            return checkNumberIfContainsKey(num / 10, key);
        }
    }
    

  • Two Sum 2022-06-19

    Given an array of unsorted numbers nums and an integer target, find two integers in the array that sum to the target and return their indices.

    There are three ways that I know of to solve this problem. Below you’ll find a description of each with some brief code examples. I would like to encourage you to try to implement your own solution first before scrolling down.

    Solution 1: Brute Force

    The first way, which is the brute force method, is to use nested loops. It tries every possible combination by looping over and take exponential time.

Recent posts
  • How to Update a File's Creation Time in PowerShell
  • How to Run PowerShell Code Only During Certain Times
  • PeopleSoft App Server Freeze Troubleshooting Checklist
  • Build Vim With Python3 Support
  • Understanding the ss Command: A Modern Alternative to netstat
© 2026 hakk
  • Home
  • Blog
  • Docs
  • DSA
  • Snippets