Posts

Showing posts with the label Sorting

In Java, Comparing And Sorting Gregorian Calendar

Image
Download

In Python, Scramble String And Check If Two Strings Are Anagram

Image
from random import shuffle def scramble_letters(txt): chars = list(txt) shuffle(chars) return ''.join(chars) def is_anagram(s1, s2): return ''.join(sorted(list(s1))) == ''.join(sorted(list(s2)))

In C, Binary Search A Number In Array

Image
#include<stdio.h> void sort(int*, const int); int binary_search(int*, const int, const int); void main(){ int my_numbers[] = { 2430,4719,2247,1397, 155,2401,3015,2324, 670,2134, 469, 952,3881,3633,3459,4366, 19, 935,1610, 724, 4373,2111,4542,1596,4244,4822,4964,1504, 462, 652, 1561,4557,4791, 387, 522 ,513,4872,4569, 241,2662, 3241,2475,3664,4028,2064,3993, 572, 649, 418,3283, 4347,3207,3349, 100,3651,4194,4725,1276,1244, 722, 2019,1232,3491, 606, 261,2054,3699,3901,1471,4477, 1569, 438,1439,3028,1839,1692,1209,4500,4996,1097, 3565,3068,2911,4416, 579,3994,1291,2914,3728,1023, 868,3652,3458,1461,4763,3904,1822,4594, 900,1763, 1645,1933,4541, 517,2676,4744,1292,4498,3848,3508, 2425, 87,2627,2532,2375,3623,3112,932,1368, 501}; const int MAX_LEN = sizeof(my_numbers)/sizeof(int); sort(my_numbers, MAX_LEN); printf("min: %d, max: %d\n", my_numbers[0], my_numbers[MAX_LEN-1]); int num = 3728; int pos = binary_search(my_numbers, MAX_LEN, num); pri...

In C, Merge Sorted Data Files Into Single Output Data File

Image
/** * your name, * the course number, * the date the program was completed, * a brief description of theprogram. */ #include<stdio.h> int merge(const char*, const char*, const char*); int main(){ int error = merge("Data1.txt","Data2.txt","Merged.txt"); if(error){ printf("Error: File merging failed\n"); } else { printf("Success: File merged\n"); } return error; } int merge(const char* n1, const char* n2, const char* n3){ FILE *in1 = fopen(n1, "r"); if(!in1){ printf("Error: Could not read file: %s\n", n1); return 1; } FILE *in2 = fopen(n2, "r"); if(!in2){ fclose(in1); printf("Error: Could not read file: %s\n", n2); return 2; } FILE *out = fopen(n3, "w"); if(!out){ fclose(in1); fclose(in2); printf("Error: Could not write file: %s\n", n3); return 3; } int num1, num2; //until end of file whi...

In Python, Test If Given List Is Sorted Or Not

Image
def isSorted(stack): value = True prv = None if 0 < len(stack): prv = stack[0] for cur in stack: if prv < cur: value = False break return value if '__main__' == __name__: stack = [20, 20, 17, 11, 8, 8, 3, 2] print(isSorted(stack), stack) stack = [22] print(isSorted(stack), stack) stack = [] print(isSorted(stack), stack) stack = [21, 22, 20] print(isSorted(stack), stack) Download

In Java, Sorted ArrayList Of Names In Title Case

Image
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static List<String> personNames = new ArrayList<String>(); public static void main(String args[]){ Scanner scanner = new Scanner(System.in); String inputName; while (true){ System.out.println("Enter the next name:"); inputName = scanner.nextLine().trim(); if (inputName.toLowerCase().equals("end")){ break; } else { insertionSort(titleCase(inputName)); } } System.out.println(personNames.toString()); } /** * * It converts the given sting in titleCase. * * @param name * * @return * */ public static String titleCase(String name){ final char[]all = name.toCharArray(); StringBuilder s1 = new StringBuilder(all.length); boolean caps = true; for(char c : all){ if(Character.isSpaceChar(c)){ caps = true; } else if(caps){ c = Character.toUpperCase(c); caps = false; ...