Eazy's Blog

Python, Android, and Algorithms

Check Anagrams in Python

This post is about verifying whether two strings are anagrams of each other.  For example: “bad credit” and “debit card” are anagrams.  This problem has been presented many times over the past years in the comp sci community, and everybody thinks they have the most efficient solution.  I have come up with the simplest solution possible in Python.  Not sure if it is the most ‘efficient’, but it’s sure as heck simple!

def isAnagram(string1, string2):
    string1_val = 0
    string2_val = 0
    for ch1 in string1:
      if (not ch1.isspace()): string1_val += int(ch1.encode('hex'), 16)
    for ch2 in string2:
      if (not ch2.isspace()): string2_val += int(ch2.encode('hex'), 16)
  
    return string1_val == string2_val

if __name__ == "__main__":
    input_string1 = raw_input ("Enter the first word/phrase: ")
    input_string2 = raw_input ("Enter the second word/phrase: ")
    if isAnagram(input_string1, input_string2): print "Is an anagram"
    else: print "Not an anagram"

Android: accessing application preferences

You can retrieve shared application preferences by using the getSharedPreferences() method of the application context:


SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);

SharedPreferences.Editor prefEditor = settings.edit();

prefEditor.putBoolean("HasCredits", true);

prefEditor.commit();

To retrieve preference settings, you simply retrieve SharedPreferences and read the values back out:


SharedPreferences settings = getSharedPreferences("GamePrefs", MODE_PRIVATE);

String userName = settings.getString("UserName", "Chippy Jr. (Default)");

~ SAMS TEACH YOURSELF ANDROID APPLICATION DEVELOPMENT IN 24 HOURS

Android scripting: print call log

The following python code will print out your call log:


import android

droid = android.Android()

myconst = droid.getConstants("android.provider.CallLog$Calls").result

calls = droid.queryContent(myconst["CONTENT_URI"],["name","number","duration"]).result

for call in calls:

    print call

~Courtesy of Pro Android Python with SL4A

Android scripting: get all available Intent constants

import android
droid = android.Android()
myconst = droid.getConstants("android.content.Intent").result
for c in myconst:
    print c, "=", myconst[c]

Should print the following:
ACTION_MANAGE_PACKAGE_STORAGE = android.intent.action.MANAGE_PACKAGE_STORAGE
ACTION_BATTERY_CHANGED = android.intent.action.BATTERY_CHANGED
ACTION_APP_ERROR = android.intent.action.APP_ERROR
ACTION_SEARCH = android.intent.action.SEARCH
ACTION_NEW_OUTGOING_CALL = android.intent.action.NEW_OUTGOING_CALL
EXTRA_KEY_CONFIRM = android.intent.extra.KEY_CONFIRM
CATEGORY_SAMPLE_CODE = android.intent.category.SAMPLE_CODE
FLAG_ACTIVITY_PREVIOUS_IS_TOP = 16777216
ACTION_TIME_TICK = android.intent.action.TIME_TICK
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY = 1048576
ACTION_INPUT_METHOD_CHANGED = android.intent.action.INPUT_METHOD_CHANGED
FLAG_ACTIVITY_REORDER_TO_FRONT = 131072
ACTION_AIRPLANE_MODE_CHANGED = android.intent.action.AIRPLANE_MODE
ACTION_UMS_DISCONNECTED = android.intent.action.UMS_DISCONNECTED
ACTION_SEND = android.intent.action.SEND
CATEGORY_CAR_DOCK = android.intent.category.CAR_DOCK
IMMUTABLE_FLAGS = 3
ACTION_CONFIGURATION_CHANGED = android.intent.action.CONFIGURATION_CHANGED
CATEGORY_OPENABLE = android.intent.category.OPENABLE
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED = 2097152
METADATA_SETUP_VERSION = android.SETUP_VERSION
CREATOR = android.content.Intent$1@40053a68
FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET = 524288
FLAG_FROM_BACKGROUND = 4
CATEGORY_BROWSABLE = android.intent.category.BROWSABLE
EXTRA_SHORTCUT_NAME = android.intent.extra.shortcut.NAME
ACTION_CLOSE_SYSTEM_DIALOGS = android.intent.action.CLOSE_SYSTEM_DIALOGS
CATEGORY_INFO = android.intent.category.INFO
CATEGORY_LAUNCHER = android.intent.category.LAUNCHER
ACTION_MEDIA_UNMOUNTABLE = android.intent.action.MEDIA_UNMOUNTABLE
ACTION_DEVICE_STORAGE_FULL = android.intent.action.DEVICE_STORAGE_FULL
FLAG_ACTIVITY_BROUGHT_TO_FRONT = 4194304
ACTION_MEDIA_SCANNER_SCAN_FILE = android.intent.action.MEDIA_SCANNER_SCAN_FILE
ACTION_DIAL = android.intent.action.DIAL
EXTRA_CHANGED_UID_LIST = android.intent.extra.changed_uid_list
EXTRA_TEXT = android.intent.extra.TEXT
FLAG_GRANT_WRITE_URI_PERMISSION = 2
ACTION_SYNC = android.intent.action.SYNC
CATEGORY_MONKEY = android.intent.category.MONKEY
EXTRA_SUBJECT = android.intent.extra.SUBJECT
EXTRA_BCC = android.intent.extra.BCC
FLAG_ACTIVITY_MULTIPLE_TASK = 134217728
FLAG_ACTIVITY_FORWARD_RESULT = 33554432
EXTRA_CLIENT_LABEL = android.intent.extra.client_label
ACTION_DEVICE_STORAGE_OK = android.intent.action.DEVICE_STORAGE_OK
FLAG_DEBUG_LOG_RESOLUTION = 8
ACTION_SHUTDOWN = android.intent.action.ACTION_SHUTDOWN
PARCELABLE_WRITE_RETURN_VALUE = 1
FLAG_ACTIVITY_NO_ANIMATION = 65536
ACTION_MEDIA_REMOVED = android.intent.action.MEDIA_REMOVED
ACTION_POWER_DISCONNECTED = android.intent.action.ACTION_POWER_DISCONNECTED
CATEGORY_HOME = android.intent.category.HOME
EXTRA_PHONE_NUMBER = android.intent.extra.PHONE_NUMBER
ACTION_PRE_BOOT_COMPLETED = android.intent.action.PRE_BOOT_COMPLETED
ACTION_PACKAGE_INSTALL = android.intent.action.PACKAGE_INSTALL
FILL_IN_SOURCE_BOUNDS = 32
ACTION_BUG_REPORT = android.intent.action.BUG_REPORT
CATEGORY_UNIT_TEST = android.intent.category.UNIT_TEST
ACTION_CAMERA_BUTTON = android.intent.action.CAMERA_BUTTON
ACTION_PACKAGE_ADDED = android.intent.action.PACKAGE_ADDED
ACTION_REBOOT = android.intent.action.REBOOT
ACTION_BATTERY_LOW = android.intent.action.BATTERY_LOW
ACTION_TIME_CHANGED = android.intent.action.TIME_SET
ACTION_MEDIA_SCANNER_FINISHED = android.intent.action.MEDIA_SCANNER_FINISHED
CONTENTS_FILE_DESCRIPTOR = 1
ACTION_DATE_CHANGED = android.intent.action.DATE_CHANGED
EXTRA_SHORTCUT_INTENT = android.intent.extra.shortcut.INTENT
EXTRA_CHANGED_PACKAGE_LIST = android.intent.extra.changed_package_list
ACTION_MAIN = android.intent.action.MAIN
ACTION_MEDIA_MOUNTED = android.intent.action.MEDIA_MOUNTED
ACTION_MEDIA_NOFS = android.intent.action.MEDIA_NOFS
ACTION_PROVIDER_CHANGED = android.intent.action.PROVIDER_CHANGED
ACTION_SEARCH_LONG_PRESS = android.intent.action.SEARCH_LONG_PRESS
CATEGORY_DESK_DOCK = android.intent.category.DESK_DOCK
ACTION_BATTERY_OKAY = android.intent.action.BATTERY_OKAY
ACTION_RUN = android.intent.action.RUN
ACTION_ANSWER = android.intent.action.ANSWER
ACTION_MEDIA_EJECT = android.intent.action.MEDIA_EJECT
ACTION_PACKAGE_CHANGED = android.intent.action.PACKAGE_CHANGED
ACTION_UID_REMOVED = android.intent.action.UID_REMOVED
ACTION_MEDIA_UNMOUNTED = android.intent.action.MEDIA_UNMOUNTED
ACTION_PICK_ACTIVITY = android.intent.action.PICK_ACTIVITY
ACTION_CREATE_SHORTCUT = android.intent.action.CREATE_SHORTCUT
ACTION_DELETE = android.intent.action.DELETE
FLAG_RECEIVER_REPLACE_PENDING = 536870912
ACTION_VOICE_COMMAND = android.intent.action.VOICE_COMMAND
FLAG_ACTIVITY_NO_USER_ACTION = 262144
ACTION_ALL_APPS = android.intent.action.ALL_APPS
ACTION_PACKAGE_REPLACED = android.intent.action.PACKAGE_REPLACED
EXTRA_REMOTE_INTENT_TOKEN = android.intent.extra.remote_intent_token
EXTRA_DOCK_STATE_MOBILE = 3
EXTRA_STREAM = android.intent.extra.STREAM
ACTION_EXTERNAL_APPLICATIONS_AVAILABLE = android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE
ACTION_SYSTEM_TUTORIAL = android.intent.action.SYSTEM_TUTORIAL
EXTRA_DATA_REMOVED = android.intent.extra.DATA_REMOVED
EXTRA_CC = android.intent.extra.CC
EXTRA_INITIAL_INTENTS = android.intent.extra.INITIAL_INTENTS
URI_INTENT_SCHEME = 1
ACTION_EDIT = android.intent.action.EDIT
ACTION_PACKAGE_RESTARTED = android.intent.action.PACKAGE_RESTARTED
ACTION_INSERT_OR_EDIT = android.intent.action.INSERT_OR_EDIT
ACTION_SENDTO = android.intent.action.SENDTO
FILL_IN_DATA = 2
CATEGORY_SELECTED_ALTERNATIVE = android.intent.category.SELECTED_ALTERNATIVE
CATEGORY_DEFAULT = android.intent.category.DEFAULT
FILL_IN_COMPONENT = 8
EXTRA_DOCK_STATE_UNDOCKED = 0
EXTRA_UID = android.intent.extra.UID
ACTION_MEDIA_SHARED = android.intent.action.MEDIA_SHARED
FLAG_GRANT_READ_URI_PERMISSION = 1
ACTION_FACTORY_TEST = android.intent.action.FACTORY_TEST
ACTION_PICK = android.intent.action.PICK
ACTION_INSERT = android.intent.action.INSERT
FLAG_RECEIVER_BOOT_UPGRADE = 134217728
EXTRA_CHANGED_COMPONENT_NAME_LIST = android.intent.extra.changed_component_name_list
ACTION_UMS_CONNECTED = android.intent.action.UMS_CONNECTED
ACTION_CALL_PRIVILEGED = android.intent.action.CALL_PRIVILEGED
ACTION_PACKAGE_REMOVED = android.intent.action.PACKAGE_REMOVED
EXTRA_TEMPLATE = android.intent.extra.TEMPLATE
EXTRA_DOCK_STATE = android.intent.extra.DOCK_STATE
ACTION_SCREEN_ON = android.intent.action.SCREEN_ON
ACTION_ALARM_CHANGED = android.intent.action.ALARM_CHANGED
ACTION_SCREEN_OFF = android.intent.action.SCREEN_OFF
ACTION_DEVICE_STORAGE_LOW = android.intent.action.DEVICE_STORAGE_LOW
EXTRA_BUG_REPORT = android.intent.extra.BUG_REPORT
CATEGORY_TEST = android.intent.category.TEST
CATEGORY_CAR_MODE = android.intent.category.CAR_MODE
ACTION_SEND_MULTIPLE = android.intent.action.SEND_MULTIPLE
ACTION_ATTACH_DATA = android.intent.action.ATTACH_DATA
EXTRA_DONT_KILL_APP = android.intent.extra.DONT_KILL_APP
ACTION_UPGRADE_SETUP = android.intent.action.UPGRADE_SETUP
FILL_IN_CATEGORIES = 4
ACTION_CALL = android.intent.action.CALL
FLAG_ACTIVITY_NO_HISTORY = 1073741824
EXTRA_EMAIL = android.intent.extra.EMAIL
CATEGORY_FRAMEWORK_INSTRUMENTATION_TEST = android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST
ACTION_USER_PRESENT = android.intent.action.USER_PRESENT
EXTRA_CLIENT_INTENT = android.intent.extra.client_intent
EXTRA_DOCK_STATE_HD = 4
ACTION_PACKAGE_DATA_CLEARED = android.intent.action.PACKAGE_DATA_CLEARED
EXTRA_SHORTCUT_ICON = android.intent.extra.shortcut.ICON
CATEGORY_TAB = android.intent.category.TAB
ACTION_POWER_CONNECTED = android.intent.action.ACTION_POWER_CONNECTED
ACTION_DEVICE_STORAGE_NOT_FULL = android.intent.action.DEVICE_STORAGE_NOT_FULL
EXTRA_SHORTCUT_ICON_RESOURCE = android.intent.extra.shortcut.ICON_RESOURCE
ACTION_VIEW = android.intent.action.VIEW
EXTRA_REPLACING = android.intent.extra.REPLACING
EXTRA_INTENT = android.intent.extra.INTENT
ACTION_GTALK_SERVICE_DISCONNECTED = android.intent.action.GTALK_DISCONNECTED
EXTRA_DOCK_STATE_DESK = 1
ACTION_TIMEZONE_CHANGED = android.intent.action.TIMEZONE_CHANGED
ACTION_CALL_EMERGENCY = android.intent.action.CALL_EMERGENCY
ACTION_HEADSET_PLUG = android.intent.action.HEADSET_PLUG
ACTION_GTALK_SERVICE_CONNECTED = android.intent.action.GTALK_CONNECTED
EXTRA_ALARM_COUNT = android.intent.extra.ALARM_COUNT
ACTION_LOCALE_CHANGED = android.intent.action.LOCALE_CHANGED
ACTION_SYNC_STATE_CHANGED = android.intent.action.SYNC_STATE_CHANGED
ACTION_MEDIA_BUTTON = android.intent.action.MEDIA_BUTTON
ACTION_CHOOSER = android.intent.action.CHOOSER
EXTRA_INSTALLER_PACKAGE_NAME = android.intent.extra.INSTALLER_PACKAGE_NAME
FLAG_ACTIVITY_NEW_TASK = 268435456
ACTION_GET_CONTENT = android.intent.action.GET_CONTENT
ACTION_SET_WALLPAPER = android.intent.action.SET_WALLPAPER
ACTION_WEB_SEARCH = android.intent.action.WEB_SEARCH
FLAG_RECEIVER_REGISTERED_ONLY = 1073741824
ACTION_BOOT_COMPLETED = android.intent.action.BOOT_COMPLETED
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS = 8388608
CATEGORY_ALTERNATIVE = android.intent.category.ALTERNATIVE
FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT = 268435456
ACTION_MEDIA_BAD_REMOVAL = android.intent.action.MEDIA_BAD_REMOVAL
FILL_IN_PACKAGE = 16
ACTION_REMOTE_INTENT = com.google.android.c2dm.intent.RECEIVE
EXTRA_CHANGED_COMPONENT_NAME = android.intent.extra.changed_component_name
ACTION_MEDIA_UNSHARED = android.intent.action.MEDIA_UNSHARED
ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE = android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE
FLAG_ACTIVITY_CLEAR_TOP = 67108864
EXTRA_DOCK_STATE_CAR = 2
EXTRA_TITLE = android.intent.extra.TITLE
FILL_IN_ACTION = 1
EXTRA_PACKAGES = android.intent.extra.PACKAGES
ACTION_CALL_BUTTON = android.intent.action.CALL_BUTTON
ACTION_MEDIA_CHECKING = android.intent.action.MEDIA_CHECKING
EXTRA_KEY_EVENT = android.intent.extra.KEY_EVENT
ACTION_WALLPAPER_CHANGED = android.intent.action.WALLPAPER_CHANGED
ACTION_MEDIA_SCANNER_STARTED = android.intent.action.MEDIA_SCANNER_STARTED
FLAG_ACTIVITY_SINGLE_TOP = 536870912
ACTION_POWER_USAGE_SUMMARY = android.intent.action.POWER_USAGE_SUMMARY
CATEGORY_EMBED = android.intent.category.EMBED
CATEGORY_DEVELOPMENT_PREFERENCE = android.intent.category.DEVELOPMENT_PREFERENCE
ACTION_DEFAULT = android.intent.action.VIEW
CATEGORY_PREFERENCE = android.intent.category.PREFERENCE
ACTION_DOCK_EVENT = android.intent.action.DOCK_EVENT
ACTION_QUERY_PACKAGE_RESTART = android.intent.action.QUERY_PACKAGE_RESTART
ACTION_REQUEST_SHUTDOWN = android.intent.action.ACTION_REQUEST_SHUTDOWN
METADATA_DOCK_HOME = android.dock_home

Insertion Sort with Python

Illustration of insertion sort for input list [5, 2, 4, 6, 1, 3]:

Python code:


def insertion_sort(my_list):
    for i in range(1, len(my_list)):
        key = my_list[i]
        print("key in my_list[" + str(i) + "]:" +  str(key))
        # Insert my_list[i] into the sorted sequence my_list[1.. i-1]
        j = i - 1
        while j >= 0 and my_list[j] > key:
            my_list[j+1] = my_list[j]
            j = j - 1
        my_list[j + 1] = key
        print("now my_list is " + str(my_list))

And to do it in reverse just change

 while j >= 0 and my_list[j] > key 

to

 while j >= 0 and my_list[j] < key 

Here is a video example with Big-O explanation:

Follow

Get every new post delivered to your Inbox.

Join 117 other followers