Saturday, May 2, 2020

Linear Search

# Search 150K lines(in a file) from 1 million lines (in a file)

BASH (236 Minutes)
#!/bin/bash for LINE in $(cat 150k-lines); do grep -x -w $LINE one-mil-lines > /dev/null 2>&1 if [[ $? -eq 0 ]]; then echo "OK -- $LINE" else echo "NOT OK -- $LINE" fi done
PYTHON (32 Minutes)
#!/usr/bin/python file = open("150k-lines", "r") fileline = file.readlines() fileone = open("one-mil-lines", "r") milline = fileone.readlines() found = 0 for line in fileline: for ol in milline: if line == ol: found = 1 break if found == 1: print "OK %s" %(line) else: print "NOT OK %s" %(line) found = 0
GOLANG (Coder :- Paul Tso) (11 Minutes)
package main import ( "bufio" "fmt" "os" "log" ) func main() { readFile1, err := os.Open("150k-lines") readFile2, err := os.Open("one-mil-lines") if err != nil { log.Fatalf("failed to open file: %s", err) } fileScanner1 := bufio.NewScanner(readFile1) fileScanner2 := bufio.NewScanner(readFile2) fileScanner1.Split(bufio.ScanLines) fileScanner2.Split(bufio.ScanLines) var fileTextLines1, fileTextLines2 []string for fileScanner1.Scan() { fileTextLines1 = append(fileTextLines1, fileScanner1.Text()) } for fileScanner2.Scan() { fileTextLines2 = append(fileTextLines2, fileScanner2.Text()) } readFile1.Close() readFile2.Close() for _, eachline1 := range fileTextLines1 { for _, eachline2 := range fileTextLines2 { if (len(eachline1) == len(eachline2) && (eachline1 == eachline2)) { fmt.Println(eachline1) } } } }
C (5 Minutes)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFF_SIZE 1000 int main(int argc, char* argv[]) { char const* const fileName = argv[1]; char const* const fileNameone = argv[2]; FILE *file = fopen(fileName, "r"); FILE *fileone = fopen(fileNameone, "rb"); fseek(fileone, 0, SEEK_END); long fsize = ftell(fileone); fseek(fileone, 0, SEEK_SET); char* fileonecontent = (char*)malloc(fsize + 1); fread(fileonecontent, 1, fsize, fileone); fclose(fileone); char line[256]; while (fgets(line, sizeof(line), file)) { if ((strstr(fileonecontent, line)) != NULL) printf("OK - %s",line); else printf("NOT OK - %s",line); } /* fileonecontent[fsize] = 0; */ free(fileonecontent); }

2 comments:

  1. Citizen Titanium Watch - ITIAN DETAILS - The Classic
    Made by a custom cost of titanium made aluminum-made premium titanium oxide formula stainless steel watch. With cerakote titanium the best in-class luxury control rocket league titanium white you can have titanium dab tool in your pocket, the

    ReplyDelete
  2. Print-in-place components make it straightforward to create mechanisms with shifting components, Powered Fans however you need to|you must} tune your 3D printer settings for one of the best results. Tolerances are crucial when 3D printing print-in-place components. Your machine must be accurate enough to depart fine spaces between shifting components, and the incorrect settings can easily leave your print welded solid.

    ReplyDelete