Login Register


[EASY] Programming Challenge filter_list
Author
Message
RE: [EASY] Programming Challenge #11
Python solution
Spoiler:
Code:
import os print("Enter file name") while(True): try: name = input() f = open(name, 'r') break except: print("\nFile could not be found. Please try again.") chars = 0 lines = 0 for line in f: lines += 1 for char in line: chars += 1 print("Chars: " + str(chars)) print("Lines: " + str(lines))
[Image: jWSyE88.png]

Reply

RE: [EASY] Programming Challenge #12
We should make this thread active again.

C -
Spoiler:
Code:
#include <stdio.h> #include <sys/stat.h> int main(int argc, char *argv[]) { if(argc!=2) { printf("Enter a filename as the first parameter.\n"); return 1; } else { struct stat st; int r = stat(argv[1],&st); if(r != 0) { printf("%s does not exist.\n",argv[1]); } else { FILE *fp = fopen(argv[1],"r"); int lines = 0; int ch; while(ch != EOF) { ch = fgetc(fp); if(ch == '\n') lines++; } printf("The number of lines in %s is %d.\n",argv[1],lines); return 0; } } }


PHP -
Spoiler:
Code:
<?php if($argc != 2) { print "Enter a filename as your second parameter.\n"; exit; } else { if(!file_exists($argv[1])) { print "File " . $argv[1] . " does not exist.\n"; exit; } else { $lines = 0; $fh = fopen($argv[1],"r"); while(!feof($fh)) { $line = fgets($fh); $lines++; } print "The number of lines in " . $argv[1] . " is " . $lines . "\n"; exit; } } ?>

Reply

RE: [EASY] Programming Challenge #13
Size doesn't matter, it's how you use it.
PHP:
PHP Code:
<?($a=file($argv[1]))?print("l:".count($a)." c:".strlen(implode("",$a))):print("err");

@meow you forgot to count chars by the way

Reply

RE: [EASY] Programming Challenge #14
(07-26-2016, 02:18 AM)Axari Wrote: Size doesn't matter, it's how you use it.
PHP:
PHP Code:
<?($a=file($argv[1]))?print("l:".count($a)." c:".strlen(implode("",$a))):print("err");

@meow you forgot to count chars by the way

Oh, whoops. All I remember reading was counting the lines. I'd fix it if I weren't lazy.

Reply

RE: [EASY] Programming Challenge #15
Here's my solution in Java:

Spoiler:
Code:
public static void main(String[] args) {        int lines = 0; //Number of lines in the text file        int chars = 0; //Number of characters in the text file        List<String> text; //Stores all the items from the text file                Scanner textFile = new Scanner(System.in);//Scanner Object                System.out.println("Path to To Text File: ");        String path = textFile.nextLine();//Read User input        File file = new File(path);                if(file.isFile()) { //Check if file exists            try(BufferedReader read = new BufferedReader(new FileReader(path))) {                                text = read.lines().collect(Collectors.toList());//Convert Stream to List                chars = text.stream().map((items) -> (items.replaceAll("\\s+", "")).length()).reduce(chars, Integer::sum);//Reduce to Chars and count                                //Print Results                System.out.println("Total Lines: " + text.size());                System.out.println("Total Characters: " + chars);                            } catch (Exception e) {                System.out.println(e);            }        } else {            System.out.println("File not found!");        }    }
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply







Users browsing this thread: 1 Guest(s)