{"id":4529,"date":"2022-08-23T06:52:48","date_gmt":"2022-08-23T01:22:48","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/"},"modified":"2022-08-23T06:52:48","modified_gmt":"2022-08-23T01:22:48","slug":"solved-confused-about-output-in-student-project","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/","title":{"rendered":"[Solved] Confused about output in student project"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-32599192\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"32599192\" data-parentid=\"32598519\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Let me ruin it for you&#8230;<\/p>\n<p>Your basic logic seems both sound and overly complex at the same time.<\/p>\n<p>Here is some input:<\/p>\n<ol>\n<li>You defined a lot of methods for tasks that already exist. Always assume that Ruby has you covered, for any task you need &#8211; even if you&#8217;re wrong, searching through the documentation will open your mind to different options at your disposal.<\/li>\n<li>You ignored the fact that the String is UTF8 encoded. so <code>c.ord + cypher<\/code> might give you an unexpected value that isn&#8217;t UTF8 valid. Also, while <code>'\u0434'.ord == 1076<\/code>, you should be aware that <code>1076.chr<\/code> would raise a <code>RangeError<\/code> exception.<\/li>\n<li>Assuming you expected ASCII values (using <code>.ord<\/code> and <code>.chr<\/code>), we should take into account the fact that bytes are limited to 8 bits (or one octet) &#8211; so the highest number for an encrypted byte is 255.<\/li>\n<li>It is better to confine the methods to a namespace using a module &#8211; which I will <strong>not<\/strong> do here, since I have no idea at what point you are in your studies.<\/li>\n<\/ol>\n<p>The Caesar algorithm:<\/p>\n<ol>\n<li>\n<p>Turn the string into an array of numbers, each representing a letter&#8230; Oh wait, there&#8217;s a built in method that does that.<a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.0\/String.html#method-i-bytes\"><code>String#bytes<\/code><\/a> returns an array of integers representing the value of each byte in the binary string.<\/p>\n<\/li>\n<li>\n<p>Iterate the array of numbers to change the array by adding the cypher number (use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.0\/Array.html#method-i-map-21\"><code>Array#map!<\/code><\/a> to change the array in-place). Here we just need to remember that bytes have an upper limit value of 255, so we should use the &#8216;modulu&#8217; method <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.0\/Fixnum.html#method-i-25\"><code>Fixnum#%<\/code><\/a>.<\/p>\n<\/li>\n<li>\n<p>Now we just need to convert the array of bytes (with the new value) back to a string&#8230; Now we know that the String has a method to turn the string into an array of bytes, so it is only logical that the inverse would also be true &#8211; I would go with the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/ruby-doc.org\/core-2.2.0\/Array.html#method-i-pack\"><code>Array#pack<\/code><\/a> method.<\/p>\n<\/li>\n<\/ol>\n<p>Here&#8217;s the gist of it as a sketch:<\/p>\n<pre><code> some_string.bytes.map! {|i| (i +\/- cypher) % 255 } .pack 'C*'\n # the +\/- refers to the question of encoding vs. decoding and won't work.\n<\/code><\/pre>\n<p>All you need is one line, really.<\/p>\n<p>I would recommend that you assume that the language you use has almost any method you would ever need. So if you need to perform an action &#8211; such as taking a unicode string and converting it into an array of numbers (vs. the limit I introduced, which is a binary octet limit of 255) &#8211; Ruby has you covered.<\/p>\n<p>Now, since my answer probably ruined the challenge to some degree &#8211; allow me to offer you back the challenge.<\/p>\n<p>Rewrite the solution I offered so that it would work for UTF8 strings (the standard encoding), allowing you a stronger encryption as the cypher&#8217;s limit will be the higher UTF8 encoding limit rather than the 255 limit for a byte.<\/p>\n<p><strong>EDIT<\/strong><\/p>\n<p>Since the &#8220;one line&#8221; concept made the answer look more advanced than it was, here&#8217;s what that code would look like if it was multi-line:<\/p>\n<pre><code># the encryption method takes a string and a number\ndef caesar_encrypt string, cypher\n   # string.bytes will return an array of numbers.\n   # The numbers are between 0 and 255 (8 bits or 1 byte)\n   # https:\/\/en.wikipedia.org\/wiki\/Byte\n   array = string.bytes\n   # using the Array.map! changes the array in place.\n   # it's the same as: array = array.map {...}\n   # the format: { ... } is a one line block, similar to the do ... end.\n   array.map! do |i|\n      (i + cypher) % 255\n   end\n   # array.pack is used for taking objects in an array and transforming them\n   # to a string. the 'C*' means we are packing bytes together to a string.\n   # There are more options such as 'N*' which would pack 32 bit numbers into\n   # a string. It's a great way to save or manipulate binary data,\n   # which is at the core of all digital data.\n   array.pack 'C*'\nend\ndef caesar_decrypt\n   caesar_encrypt string, (0-cypher)\nend\n<\/code><\/pre>\n<p>Notice that <code>Array#pack<\/code> might be a bit advanced and using your original solution will also work&#8230; now that we are using bytes, <code>Fixnum#chr<\/code> should work as expected.<\/p>\n<pre><code>def caesar_encrypt string, cypher\n   array = string.bytes\n   array.map! do |i|\n      (i + cypher) % 255\n   end\n   out = \"\"\n   array.each {|c| out &lt;&lt; c.chr}\n   out\nend\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">3<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Confused about output in student project <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Let me ruin it for you&#8230; Your basic logic seems both sound and overly complex at the same time. Here is some input: You defined a lot of methods for tasks that already exist. Always assume that Ruby has you covered, for any task you need &#8211; even if you&#8217;re wrong, searching through the &#8230; <a title=\"[Solved] Confused about output in student project\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\" aria-label=\"More on [Solved] Confused about output in student project\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[455],"class_list":["post-4529","post","type-post","status-publish","format-standard","hentry","category-solved","tag-ruby"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Confused about output in student project - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Confused about output in student project - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Let me ruin it for you&#8230; Your basic logic seems both sound and overly complex at the same time. Here is some input: You defined a lot of methods for tasks that already exist. Always assume that Ruby has you covered, for any task you need &#8211; even if you&#8217;re wrong, searching through the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-23T01:22:48+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Confused about output in student project\",\"datePublished\":\"2022-08-23T01:22:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\"},\"wordCount\":505,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"ruby\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\",\"name\":\"[Solved] Confused about output in student project - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-23T01:22:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Confused about output in student project\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Confused about output in student project - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Confused about output in student project - JassWeb","og_description":"[ad_1] Let me ruin it for you&#8230; Your basic logic seems both sound and overly complex at the same time. Here is some input: You defined a lot of methods for tasks that already exist. Always assume that Ruby has you covered, for any task you need &#8211; even if you&#8217;re wrong, searching through the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/","og_site_name":"JassWeb","article_published_time":"2022-08-23T01:22:48+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Confused about output in student project","datePublished":"2022-08-23T01:22:48+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/"},"wordCount":505,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["ruby"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/","url":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/","name":"[Solved] Confused about output in student project - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-23T01:22:48+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-confused-about-output-in-student-project\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Confused about output in student project"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=4529"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4529\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}