Skip to content

Instantly share code, notes, and snippets.

@etheleon
Last active March 30, 2019 11:28
Show Gist options
  • Save etheleon/30192c979e7d1f93f469dd00bdc97f22 to your computer and use it in GitHub Desktop.
Save etheleon/30192c979e7d1f93f469dd00bdc97f22 to your computer and use it in GitHub Desktop.
beedrive/palindrome.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "markdown",
"source": "# Learning Julia\n\ngist-id: 30192c979e7d1f93f469dd00bdc97f22 \n\n## Palindrome\n\nGiven the string, check if it is a palindrome.\n\n* For `inputString = \"aabaa\"`, the output should be\n`checkPalindrome(inputString) = true;`\n* For `inputString = \"abac\"`, the output should be\n`checkPalindrome(inputString) = false;`\n* For `inputString = \"a\"`, the output should be\n`checkPalindrome(inputString) = true`.\n\n\n* Execution time limit: 7 seconds (jl)\n* Input: string inputString\n\nA non-empty string consisting of lowercase characters."
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-03-30T11:21:04.129Z",
"end_time": "2019-03-30T19:21:04.363000+08:00"
},
"trusted": true
},
"cell_type": "code",
"source": "alist = collect(1:10)\nInt(length(alist) / 2)",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-03-30T11:24:27.837Z",
"end_time": "2019-03-30T19:24:28.619000+08:00"
},
"trusted": true
},
"cell_type": "code",
"source": "#reverse(alist[5:end])\njoin(alist[5:end])",
"execution_count": 35,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 35,
"data": {
"text/plain": "\"5678910\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-03-30T11:26:15.672Z",
"end_time": "2019-03-30T19:26:15.893000+08:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Int(trunc(5 / 2))",
"execution_count": 42,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 42,
"data": {
"text/plain": "2"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-03-30T11:27:05.294Z",
"end_time": "2019-03-30T19:27:05.592000+08:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function checkPalindrome(inputString::String)\n alist = split(inputString, \"\")\n string_length = length(alist)\n if string_length == 1\n return true\n else \n is_even = string_length % 2 == 0\n if is_even\n println(\"is even\")\n middle = Int(length(alist) / 2)\n first = join(inputString[1:middle])\n second = join(reverse(inputString[middle+1:end]))\n if first == second\n return true\n else\n return false\n end\n else\n println(\"not even\")\n middle = Int(trunc(length(alist) / 2))\n first = join(inputString[1:middle])\n second = join(reverse(inputString[middle+2:end]))\n if first == second\n return true\n else\n return false\n end\n end\n return false\n end\nend",
"execution_count": 44,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 44,
"data": {
"text/plain": "checkPalindrome (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-03-30T11:27:06.650Z",
"end_time": "2019-03-30T19:27:06.940000+08:00"
},
"trusted": true
},
"cell_type": "code",
"source": "checkPalindrome(\"abba\")",
"execution_count": 45,
"outputs": [
{
"output_type": "stream",
"text": "is even\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 45,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2019-03-30T11:27:13.840Z",
"end_time": "2019-03-30T19:27:14.062000+08:00"
},
"trusted": true
},
"cell_type": "code",
"source": "checkPalindrome(\"abgba\")",
"execution_count": 46,
"outputs": [
{
"output_type": "stream",
"text": "not even\n",
"name": "stdout"
},
{
"output_type": "execute_result",
"execution_count": 46,
"data": {
"text/plain": "true"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.1",
"display_name": "Julia 1.1.0",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.1.0"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "30192c979e7d1f93f469dd00bdc97f22",
"data": {
"description": "beedrive/palindrome.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/30192c979e7d1f93f469dd00bdc97f22"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment