[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Can /g/ do this?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 27
Thread images: 3

Write a function that takes a list of strings an prints them, one per line, in a rectangular frame. For example the list ["Hello", "World", "in", "a", "frame"] gets printed as:

*********
* Hello *
* World *
* in *
* a *
* frame *
*********
>>
*********
* Hello *
* World *
* in *
* a *
* frame *
*********

Asterisks need to be inline
>>
>>61523598
You can't be serious this is shit easy goy
>>
Iterate over all elements to find the max length. Then pad all the shorter strings with the required number of spaces to match that length. Pretty simple
>>
>>61523598
>Find the widest string.
>Format the rest of the prints to be the same width as widest string

Wow that was hard
>>
Ask /sci/, they are the homework board.
>>
>>61523598
max_len = len(max(mah_list, key=len)) + 2
for i in mah_list:
if i == mah_list[0]:
print(max_len * '*')
print('*' + i + (' ' * (max_len - (len(i) - 1))) + '*')


Not sure if it works.
t.retard on the phone
>>
>>61523740
>>61523748
>>61523777
>>61523787
I have already done it guys. I just want to see what it would look like in other languages.
>>
>>61523619
>>61523598
use code tags retard

>>61523740
>>61523748
this
>>
>>61523841
print("Dont be so angry anon")
>>
>>61523794
Kek
Remove that print '*' from loop and add one before and after.

t.same phone retard
>>
File: IMG_1011.png (165KB, 750x1334px) Image search: [Google]
IMG_1011.png
165KB, 750x1334px
>>61523941
>>61523794
He doesn't write code on his phone. Ahaha oh wow.
>>
>>61523968
I might be stupid, but not that stupid...
>>
>>61523968
>>61523987
Or better yet, I don't hate myself that much.
>>
#!/bin/bash
frame_start() {
declare -i i
for (( i=0; i<$1; i++ )); do
echo -n '*'
done
echo
}

# find max len
declare -i max_len
for word in "$@"; do
if (( ${#word} > max_len )); then
max_len=${#word}
fi
done

frame_start "$(( max_len + 4 ))"
printf '* %'"$max_len"'s *\n' "$@"
frame_start "$(( max_len + 4 ))"


it's not left justified but whatever, sue me. it would just mean another for loop and some printf indecipherable formatting cancer like %s%-*i "string" some_num.
>>
>>61523968
That is really poorly programmed.
>>
count=0    
for str in ["Hello", "World", "in", "a", "frame"]:
if len(str)>0 :
count=count+1
k=9-len(str)
# print(k)
if count==1:
print("***********")
print("*"+str+(" "*k)+"*" )
print("***********")


It just werks :^)
>>
public class StringRectangle {
public static void main(String[] args) {
int max = 0;
for(int i = 0;i<args.length;i++)
max = args[i].length() > max ? args[i].length():max;
for(int i = 0;i<max+4;i++)
System.out.print('*');
for(int i = 0;i<args.length;i++)
System.out.printf("\n* %-"+max+"s *",args[i]);
System.out.println();
for(int i = 0;i<max+4;i++)
System.out.print('*');
System.out.println();
}
}
>>
>>61524032
Yeah i know, its it's pretty fucking bad. It works though, and that counts for something. I guess.
>>
>>61524090
import java.util.Scanner;

public class Bin2Dec {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a binary number for it's decimal equivalent: ");
String number = input.nextLine();
System.out.println(bin2Dec(number));
}

public static int bin2Dec(String number) {
int result = 0;
for(int i = 0;i<number.length();i++)
result = (result<<1) + number.charAt(i)-'0';
return result;
}
}
>>
#!/usr/bin/env ruby

def encase(words)
max_length = words.max_by {|w| w.length }.length
puts '*'*(max_length + 4)
words.each {|w| puts "* #{w.ljust(max_length)} *" }
puts '*'*(max_length + 4)
end

encase(ARGV)

$ ./encase.rb hello world in a frame
*********
* hello *
* world *
* in *
* a *
* frame *
*********
>>
Bumping for other languages
>>
printf "* %-${max_len}s *\n" "$@"
>>
>>61526432
for >>61524029
>>
#!/usr/bin/python3
import sys
s=sys.argv[1:]
ml=max([len(x) for x in s])
f="*"*(ml+2)
print(f+"\n"+"\n".join(["*"+x+" "*(ml-len(x))+"*" for x in s])+"\n"+f)


Not sure if it can be even more shortened.
>>
File: 1458691752132.gif (214KB, 274x249px) Image search: [Google]
1458691752132.gif
214KB, 274x249px
>>61523787
KISAMAAA
>>
C
for some reason my code is blocked.
https://hastebin.com/qagicuguda.c
Thread posts: 27
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.