Created
November 13, 2015 13:37
-
-
Save BurcakAsal/5c438b00dc55d94030ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"#Selection Sort\n", | |
"def func_min(new_list):\n", | |
" \n", | |
" min=new_list[0]\n", | |
" \n", | |
" for i in range(1,len(new_list)):\n", | |
" if(new_list[i]<min):\n", | |
" min=new_list[i]\n", | |
" return (min)\n", | |
"\n", | |
"\n", | |
" \n", | |
"def selection_sort(org_list):\n", | |
" sorted_list = []\n", | |
" while(len(org_list)):\n", | |
" sorted_list.append(func_min(org_list))\n", | |
" org_list.remove(func_min(org_list))\n", | |
" return sorted_list \n", | |
" \n", | |
" \n", | |
"array = [20, 21, 23, 35 ,22, 19, 18, -3 ,14, 4]\n", | |
"\n", | |
"\n", | |
"\n", | |
"print(array)\n", | |
"s_array = selection_sort(array)\n", | |
"print(s_array)\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Creating a dictionary and sends its keys to Set A or Set B \n", | |
"def mod_nm(number,divider):\n", | |
" while(number>=divider):\n", | |
" number-=divider\n", | |
" \n", | |
" return number\n", | |
" \n", | |
"def is_prime(num):\n", | |
" \n", | |
" i=2\n", | |
" while(i<=num/2):\n", | |
" if(mod_nm(num,i)==0):\n", | |
" return 0\n", | |
" else:\n", | |
" i=i+1\n", | |
" return 1\n", | |
"\n", | |
"def prime_set(dict_p):\n", | |
" \n", | |
" for e_key in dict_p.keys():\n", | |
" if(is_prime(dict_p[e_key])):\n", | |
" set_B.add(e_key)\n", | |
" else:\n", | |
" set_A.add(e_key)\n", | |
" \n", | |
" \n", | |
"set_A = set([])\n", | |
"set_B = set([])\n", | |
" \n", | |
"new_dict={}\n", | |
"\n", | |
"new_dict[1]= 17 \n", | |
"new_dict[2]= 29 \n", | |
"new_dict[3]= 38 \n", | |
"new_dict[4]= 49 \n", | |
"new_dict[5]= 125 \n", | |
"new_dict[6]= 37 \n", | |
"new_dict[7]= 53 \n", | |
"new_dict[8]= 86 \n", | |
"new_dict[9]= 93 \n", | |
"\n", | |
"\n", | |
"prime_set(new_dict)\n", | |
"\n", | |
"print(new_dict)\n", | |
"print(set_A)\n", | |
"print(set_B)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Finding a set including elements that appear more than once in a list \n", | |
"def more_than_once(original_list):\n", | |
" \n", | |
" unique_set = set(original_list)\n", | |
" \n", | |
" for item in unique_set:\n", | |
" original_list.remove(item) \n", | |
" \n", | |
" return set(original_list)\n", | |
" \n", | |
" \n", | |
"list_new = [1, 5 ,3, 2, 5 , 4 , 7 , 8 , 3, 1, 1]\n", | |
"\n", | |
"print(list_new) \n", | |
"\n", | |
"set_new = more_than_once(list_new)\n", | |
"\n", | |
"print(set_new)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"#Function that reverse a list\n", | |
"def reverse_list(original_list):\n", | |
" reversed_list = []\n", | |
" \n", | |
" for i in range(len(original_list)-1,-1,-1):\n", | |
" reversed_list.append(original_list[i])\n", | |
" \n", | |
" return reversed_list\n", | |
" \n", | |
"new_list = [5,3,9,7,13,136,45,22]\n", | |
"\n", | |
"print(reverse_list(new_list))\n", | |
"print(new_list)\n", | |
" " | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment